- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we change a field name in JSON using Jackson in Java?
The Jackson Annotation @JsonProperty is used on a property or method during serialization or deserialization of JSON. It takes an optional ‘name’ parameter which is useful in case the property name is different than ‘key’ name in JSON. By default, if the key name matches the property name, value is mapped to property value.
In the below example, we can change a field name in JSON using @JsonProperty annotation.
Example
import java.io.IOException; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.JsonProperty; public class JsonPropertyAnnotationTest { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); User user = new User("Sai", "Adithya", "9959984000", "0402358700"); String data = mapper.writeValueAsString(user); System.out.println(data); } } // User class class User { @JsonProperty("first-name") public String firstName; @JsonProperty("last-name") public String lastName; @JsonProperty("mobile-phone") public String mobilePhone; @JsonProperty("home_phone") public String workPhone; public User(String firstName, String lastName, String mobilePhone, String workPhone) { super(); this.firstName = firstName; this.lastName = lastName; this.mobilePhone = mobilePhone; this.workPhone = workPhone; } }
Output
{ "first-name" : "Sai", "last-name" : "Adithya", "mobile-phone" : "9959984000", "home_phone" : "0402358700" }
- Related Articles
- How can we convert a JSON array to a list using Jackson in Java?\n
- How to ignore a field of JSON object using the Jackson library in Java?\n
- JSON Schema Support using Jackson in Java?
- How can we format a date using the Jackson library in Java?
- How to create a JSON using Jackson Tree Model in Java?
- How can we map multiple date formats using Jackson in Java?
- How can we create a JSON using JsonGenerator in Java?
- Pretty print JSON using Jackson library in Java?
- How to search a value inside a JSON file using Jackson in Java?
- How to convert a JSON object to an enum using Jackson in Java?
- How can we implement a JSON array using Streaming API in Java?
- How to define alternate names to a field using Jackson in Java?
- How to convert a JSON to Java Object using the Jackson library in Java?\n
- How to convert Java object to JSON using Jackson library?
- Convert JSON to/from Map using Jackson library in Java?

Advertisements