How can I find the JSON key of a field (jackson)?
<p>I have the following problem: I export my user object as usual with the <code>jackson.databind.ObjectMapper</code> and that works fine: <code>({"address":{"village":"NY"},"prename":"Joe"})</code>.</p>

<p>Now I have to get the key (for <code>address</code> and <code>prename</code>) with Java reflection.
If the field has the annotation <code>@JsonProperty</code>, there is no problem to get this key. But this annotation isn't pressent on all fields (for example the <code>m_address</code> field).</p>

<p>At <a href="https://stackoverflow.com/questions/22162916/how-does-the-jackson-mapper-know-what-field-in-each-json-object-to-assign-to-a-c">How does the Jackson mapper know what field in each Json object to assign to a class object?</a> I read that the <code>ObjectMapper</code> tries to call the getter or so.</p>

<p>But I have no clue how I can find the right getter to my field.</p>

<p>I know that this isn't probably the most beautiful way to solve my problem, but I haven't found any method on the <code>ObjectMapper</code> like: <code>mapper.getJSONKeyByName(field)</code>.
If something like that exist even better. :)</p>

<p>Is there a way to find the right getter to a field and does something like <code>mapper.getJSONKeyByName(field)</code> exist on the <code>ObjectMapper</code>?</p>

<p>Main.java</p>

<pre><code>public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();   

    // Object to JSON as usual
    mapper.writeValue(System.out, new User("Joe", new Address("NY")));
    // {"address":{"village":"NY"},"prename":"Joe"}


    // Lookup with reflection
    for (Field field : User.class.getDeclaredFields()) {
        field.setAccessible(true);
        try {
            if (field.isAnnotationPresent(JsonProperty.class)) {
                System.out.println("JSON-Key with annotation: " +
                    field.getAnnotation(JsonProperty.class).value());
                    // JSON-Key with annotation: prename
            } else {
                //TODO do something to get "JSON-Key without annotation: address
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
</code></pre>

<p>User.java</p>

<pre><code>public class User implements Serializable {
    @JsonProperty(value="prename")
    @Validationinfo(name="prename", required=true, maxlenght=20)
    private String m_name;
    private Address m_address;

    public User(String name, Address a) {
        m_name = name;
        m_address = a;
    }

    @JsonIgnore
    public String getName() {
        return m_name;
    }

    public void setName(String name) {
        m_name = name;
    }

    public Address getAddress() {
        return m_address;
    }

    public void setAddress(Address address) {
        m_address = address;
    }
}
</code></pre>

<p>Address.java</p>

<pre><code>public class Address implements Serializable {

  @JsonProperty(value="village")
  @Validationinfo(name="village", required=false, maxlenght=10)
  private String m_village;

  public Address(String village) {
    m_village = village;
  }

  public String getVillage() {
    return m_village;
  }

  public void setVillage(String village) {
    m_village = village;
  }
}
</code></pre>

<p>EDIT:</p>

<p>The code is simplified. I have a REST service which does the <code>writeValue </code> part. The reflection part is done in a static recursive method on the <code>User</code>.</p>

<p>The thing is I have a custom <code>Validationinfo</code> annotation (with things like required, maxlength and so on) on my fields and also a name parameter. This name is the same as it is on the <code>@JsonProperty</code> annotation.</p>

<p>On an JavaScript application I want  merge the value of the user fields with the <code>ValidationInfos</code>. For that I have to ensure that every <code>validationInfos->name</code> is unique. So I have to prefix the annotated <code>Vaditioninfo->name</code> whith the JSON serialized name/key of its parent (see in the REST respons "address.village").</p>

<p>The rest response I am locking for:</p>

<pre><code>  {
    "user": {
       "prename": "Joe",
       "address" : {
          "village": "NY"
       }
     }, "validationInfos": [{
          "name": "prename",
          "required": true,
          "maxlenght": 10
         }, {
          "name": "address.village",
          "required": false,
          "maxlenght": 20
         }]
  }
</code></pre>

<p>In JavaScript I planning to do something like:</p>

<pre><code>for (var i = 0; i &lt; data.validationInfos.length;; i++) {
  var element = data.validationInfos;
  element.value = eval ("data.user." + element.name);
}
</code></pre>