In this quick tutorial, I show you how to change the name of a field to map to another JSON property on serialization. Jackson library provides @JsonProperty annotation that is used to change the property name in serialized JSON.
Dependencies
Let’s first add the following dependencies to the pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.
Change Name of Field for Serialization
1. Without using @JsonProperty Annotation
Let's first create a simple Java class and test it without adding @JsonProperty annotation.
package net.javaguides.jackson.annotations;
public class User {
public int id;
private String firstName;
private String lastName;
private String fullName;
public User(int id, String firstName, String lastName, String fullName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.fullName = fullName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
Let's test above code with main() method:
package net.javaguides.jackson.annotations;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonPropertyAnnotationTest {
public static void main(String[] args) throws IOException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
String result = mapper.writeValueAsString(bean);
System.out.println(result);
}
}
Output:
{
"id" : 1,
"firstName" : "Ramesh",
"lastName" : "Fadatare",
"fullName" : "Ramesh Fadatare"
}
Look at the output, if you don't use @JsonProperty annotation on class fields at all then property name will be same as a getter and setter methods of fields in the class.
2. Using @JsonProperty Annotation
Let's add a @JsonProperty annotation to User class fields to customize the output like:
{
"id" : 1,
"first_name" : "Ramesh",
"last_name" : "Fadatare",
"full_name" : "Ramesh Fadatare"
}
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
public int id;
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@JsonProperty("full_name")
private String fullName;
public User(int id, String firstName, String lastName, String fullName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.fullName = fullName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
Now, let's test above code with main() method:
package net.javaguides.jackson.annotations;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonPropertyAnnotationTest {
public static void main(String[] args) throws IOException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
String result = mapper.writeValueAsString(bean);
System.out.println(result);
}
}
Output:
{
"id" : 1,
"first_name" : "Ramesh",
"last_name" : "Fadatare",
"full_name" : "Ramesh Fadatare"
}
Related Articles
- Change Field Name in JSON using Jackson (popular)
Comments
Post a Comment
Leave Comment