How to map JSON field names to different object field names?

Probably it's a bit late but anyway..

you can rename a property just adding

@JsonProperty("contractor")

And by default Jackson use the getter and setter to serialize and deserialize.

For more detailed information: http://wiki.fasterxml.com/JacksonFAQ


Please note that the proper JavaEE API for this is to use the javax.json.bind.annotation.JsonbProperty annotation. Of course Jackson and others are just some implementations of the JSON Binding API, they will likely comply with this.


With some example, You can also use it in getter and setter to rename it to different field

public class Sample {

    private String fruit;

    @JsonProperty("get_apple")
    public void setFruit(String fruit) {
        this.fruit = fruit;
    }

    @JsonProperty("send_apple")
    public String getFruit() {
        return fruit;
    }

}

If you are not using Jackson still want to rename a property you can use @SerializedName("your_original_key_name")

My JSON Data:

{
  "default": "0"
}

As you know we never use predefined keywords as a variable name so solution is:

@SerializedName("default")
private String default_value;

public String getDefault_value() {
        return default_value;
    }
public void setDefault_value(String default_value) {
        this.default_value = default_value;
    }

That's all you have to do now value comes from the key "default" and you can use it with getter and setter using "default_value"

In this case (Predifind Keywords as Json Key Name) or in any other case where you want to change your variable name to get data from the original key name this is the easiest approach.