How to change a field name in JSON using Jackson
Codemia Knowledge Hub ·
Introduction
When working with JSON data in Java applications, Jackson is a powerful library that facilitates serialization and deserialization of JSON objects. Occasionally, you might need to change the name of a field in a JSON object to maintain compatibility with different API specifications or to adapt to naming conventions. In this article, we will delve into how to change a field name in JSON using the Jackson library, covering technical aspects and providing examples.
Jackson Overview
Jackson is a suite of data-processing tools for Java that includes the following:
- ObjectMapper: The core component that enables conversion between Java objects and JSON.
- Annotations: Simplify the configuration of serialization and deserialization processes.
- Modules and Extensions: Enhance Jackson's capabilities with additional features.
JSON field name customization is typically achieved by leveraging Jackson annotations, allowing seamless remapping of field names during object binding.
Renaming JSON Fields
Using @JsonProperty Annotation
The @JsonProperty annotation is one of the simplest ways to change the field name while serializing or deserializing JSON data.
Explanation
- Constructor Argument: By using
@JsonProperty("original_name")on the constructor, Jackson maps the JSON fieldoriginal_nameto theoriginalNameproperty during deserialization. - Getter Method: Similarly, applying
@JsonProperty("original_name")on the getter method ensures that serialization converts theoriginalNameproperty tooriginal_name.
Serializing and Deserializing
To serialize and deserialize using ObjectMapper, follow these steps:
Expected Output
Advanced Considerations
Using Mix-ins
If it's not possible to modify the source class or if you want a non-intrusive approach, use Jackson mix-ins. A mix-in is an abstract class or interface used to add annotations without altering the target class.
Practical Example
Expected Output
Summary Table
| Feature | Approach | Advantages |
| Basic Renaming | @JsonProperty | Simple and intuitive Directly on class |
| Non-Intrusive Renaming | Mix-ins | Allows for separation of concerns |
| Custom Serialization Rules | Custom Serializer/Deserializer annotations | Control over serialization logic |
Conclusion
Changing JSON field names in a Java application with Jackson can be achieved using various methods to suit different use cases. Whether through @JsonProperty, mix-ins, or other advanced serialization techniques, Jackson provides flexibility and control over JSON processing. By understanding and utilizing these tools, developers can efficiently manage JSON data within their projects.
Related reading
- How to change basePath for Springfox Swagger 2.0
- How to change embebed-tomcat default port using spring boot?
- How to change font size in Eclipse for Java text editors?
- How to change root logging level programmatically for logback
- How to change Spring Boot logo to Nyan Cat?
- How to change the decimal separator of DecimalFormat from comma to dot/point?
- How to change the port of a Spring Boot application using Gradle?
- How to check certificate name and alias in keystore files?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.