Change Field Name in JSON Using Jackson in Java



When we have a JSON object and want to change a specific field name for some reason, we can use the Jackson library in Java.

The Jackson Annotation @JsonProperty

The Jackson Annotation @JsonProperty is used on a property or method during the serialization or deserialization of JSON. It takes an optional 'name' parameter, which is useful in case the property name is different from the key name in JSON.

By default, if the key name matches the property name, the value is mapped to the property value. If the key name is different, we can use the @JsonProperty annotation to specify the key name in JSON.

To use the Jackson library, we need to add it to our project. If you are using Maven, add this to your pom.xml file:

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.14.0</version>
</dependency>

Add the jar file to your project if you do not use Maven. You can download the jar file from here.

Steps to Change a Field Name in JSON

Following are the steps to change a field name in JSON using the Jackson library:

  • Add the Jackson library to your project.
  • Create a Person class with fields like name and age.
  • Use @JsonProperty("full_name") above the name field to rename it in JSON.
  • Create a Person object and set some values.
  • Use ObjectMapper to convert it to a JSON string with writeValueAsString().
  • Print the JSON ? the field name will appear as full_name.
  • Use readValue() to convert the JSON back to a Person object.
  • Print the object to verify it's working.

Example

Following is an example to change a field name in JSON using the Jackson library. Here, we will change the field name to full_name in JSON using the Jackson library -

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;

public class ChangeFieldNameExample {
   public static void main(String[] args) throws Exception {
      // Create a Person object
      Person person = new Person("Ansh", 23);
      // Create an ObjectMapper object
      ObjectMapper objectMapper = new ObjectMapper();
      // Enable pretty printing
      objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
      // Convert the Person object to JSON string
      String jsonString = objectMapper.writeValueAsString(person);
      // Print the JSON string
      System.out.println("JSON String: " + jsonString);
      // Convert JSON string back to Person object
      Person personFromJson = objectMapper.readValue(jsonString, Person.class);
      // Print the Person object
      System.out.println("Person Object: " + personFromJson.getName() + ", " + personFromJson.getAge());
   }
}
class Person {
   @JsonProperty("full_name")
   private String name;
   private int age;

   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }

   public String getName() {
      return name;
   }

   public int getAge() {
      return age;
   }
}

The output of the above code will be:

JSON String: {
  "full_name" : "Ansh",
  "age" : 23
}
Person Object: Ansh, 23
Updated on: 2025-05-12T14:02:30+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements