@JsonProperty annotation is used to change the field name while serializing or deserializing the JSON. If @JsonProperty annotation is not used on a field, the field name will be treated as the JSON key name, and the same will appear in the JSON.
We can use the @JsonProperty annotation either on the field or the getter or setter method.
private String firstName;
@JsonProperty("test")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
It will work and “test” will the property name
private String firstName;
public String getFirstName() {
return firstName;
}
@JsonProperty("test")
public void setFirstName(String firstName) {
this.firstName = firstName;
}
It will also work
@JsonProperty("test")
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
It will work too
Serialization:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Person {
@JsonProperty("test")
private String firstName;
private String lastName;
private int age;
public Person() {
super();
}
public Person(String firstName, String lastName, int age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@JsonProperty("PersonAge")
public int getPersonAge() {
return age;
}
public void setPersonAge(int age) {
this.age = age;
}
public static void main(String[] args) throws JsonProcessingException {
Person person = new Person();
person.setFirstName("Codekru");
person.setLastName("Startup");
person.setPersonAge(1);
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person));// serializing object
}
}
@JsonProperty is used at firstName property and the getter method of the age field. And in both scenarios, the name assigned to the fields will be reflected in the resulting JSON.
Output:
{
"lastName" : "Startup",
"test" : "Codekru",
"PersonAge" : 1
}
Deserialization:
We have serialized the object, and in the same way, we can also work on the deserialization. Here we have used the same Person object in the Serialization example. In deserialization, we will provide JSON and convert it into an object.
public static void main(String[] args) throws JsonProcessingException {
String personJson = "{\"test\" : \"Codekru\", \"PersonAge\" : 20}";
ObjectMapper mapper = new ObjectMapper();
Person person = mapper.readValue(personJson, Person.class);// deserializing object
System.out.println(person.getPersonAge()); // 20
System.out.println(person.getFirstName()); // Codekru
}
Output –
20
Codekru
The test JSON key is mapped to the firstName field, and the PersonAge JSON key is mapped to the age field.
We hope that you find this post helpful.
Thank you for reading this article. If you find anything wrong or have any queries, you can write in the comments or mail us at admin@codekru.com.
Other Jackson Annotations –
- @JsonIgnoreProperties and @JsonIgnore annotations
- @JsonPropertyOrder Jackson Annotation
- @JsonRawValue Jackson Annotation
- @JsonValue Jackson Annotation
- @JsonAnyGetter Jackson Annotation
- @JsonAnySetter Jackson Annotation
- Java Optional with Jackson
- @JsonInclude Jackson Annotation
- @JsonFormat Jackson Annotation