Jackson Change Property Name
October 17, 2017
This page will walk through how to change property name using Jackson API. Jackson provides @JsonProperty
annotation that is used to change property name in serialized JSON. @JsonProperty
is annotated on class fields and we need to pass a value that will be used as property name for that field in serialized JSON. If we do not pass value to @JsonProperty
then property name in serialized JSON will be same as class field name. If we do not use @JsonProperty
annotation on class fields at all then property name will be same as class field name in serialized JSON. On this page we will create examples to covert Java object into JSON and also we will provide example to convert JSON into Java object. Our java bean will use @JsonProperty
annotation to change property name in our examples.
Contents
Gradle to Resolve Jackson API
Find the Gradle file to resolve Jackson API.build.gradle
apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'Concretepage' version = '1' repositories { mavenCentral() } dependencies { compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.1' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.1' compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.1' }
@JsonProperty
Jackson provides@JsonProperty
annotation that can be used to change property name while serializing Object to JSON. Let us understand step by step.
1. Find sample code.
public class Book { @JsonProperty("bookName") private String name; @JsonProperty("bookCategory") private String category; ------ }
@JsonProperty
annotation and we are assigning a value. When we serialize the object of Book
class, we will get JSON whose properties name will be as defined by @JsonProperty
values.
{ "bookName" : "Learning Java", "bookCategory" : "Java" }
@JsonProperty
annotation but do not provide value then the serialized JSON will contain property name as class field name. By default @JsonProperty
has value (""). It indicates that field name will be used as property name while serializing JSON. Suppose we are using @JsonProperty
as given below.
public class Book { @JsonProperty private String name; @JsonProperty private String category; ------ }
{ "name" : "Learning Java", "category" : "Java" }
@JsonProperty
at all on the class fields then it will act as default behavior of @JsonProperty
. It means the serialized JSON output will contain property name as class field name. Find the sample code.
public class Book { private String name; private String category; ------ }
{ "name" : "Learning Java", "category" : "Java" }
Example 1: Convert Java Object to JSON
Find the example to convert Java Object to JSON using Jackson. In this example we will change property name using@JsonProperty
.
Book.java
package com.concretepage; import com.fasterxml.jackson.annotation.JsonProperty; public class Book { @JsonProperty("bookName") private String name; @JsonProperty("bookCategory") private String category; public Book(){} public Book(String name, String category) { this.name = name; this.category = category; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } }
package com.concretepage; import com.fasterxml.jackson.annotation.JsonProperty; public class Writer { @JsonProperty("writerId") private Integer id; @JsonProperty("writerName") private String name; @JsonProperty("writerBook") private Book book; public Writer(){} public Writer(Integer id, String name, Book book){ this.id = id; this.name = name; this.book = book; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } }
package com.concretepage; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class ObjectToJSON { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); Book book = new Book("Learning Java", "Java"); Writer writer = new Writer(110, "Mohit", book); String jsonWriter = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(writer); System.out.println(jsonWriter); } }
{ "writerId" : 110, "writerName" : "Mohit", "writerBook" : { "bookName" : "Learning Java", "bookCategory" : "Java" } }
@JsonProperty
annotated at class fields.
If we do not use
@JsonProperty
annotation on the fields of Book
and Writer
then serialized JSON output will contain property names as class field names as given below.
{ "id" : 110, "name" : "Mohit", "book" : { "name" : "Learning Java", "category" : "Java" } }
Example 2: Convert JSON to Java Object
In this example we will convert JSON to Java Object using Jackson.@JsonProperty
annotation is being used on the fields of Book
and Writer
classes. Find the example.
JSONToObject.java
package com.concretepage; import java.io.IOException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JSONToObject { public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { String jsonData = "{" +"\"writerId\" : 111," +"\"writerName\" : \"Mahesh\"," +"\"writerBook\" : {" +"\"bookName\" : \"Learning Spring\"," +"\"bookCategory\" : \"Spring\"" +"}" +"}"; ObjectMapper mapper = new ObjectMapper(); Writer writer = mapper.readValue(jsonData, Writer.class); System.out.println(writer.getId()+", "+ writer.getName()); Book book = writer.getBook(); System.out.println(book.getName()+", "+ book.getCategory()); } }
111, Mahesh Learning Spring, Spring