Jackson – Change Name of Field

1. Overview

This quick tutorial illustrates how to change the name of a field to map to another JSON property on serialization.

If you want to dig deeper and learn other cool things you can do with the Jackson 2 – head on over to the main Jackson tutorial.

2. Change Name of Field for Serialization

Working with a simple entity:

public class MyDto {
    private String stringValue;

    public MyDto() {
        super();
    }

    public String getStringValue() {
        return stringValue;
    }

    public void setStringValue(String stringValue) {
        this.stringValue = stringValue;
    }
}

Serializing it will result in the following JSON:

{"stringValue":"some value"}

To customize that output so that, instead of stringValue we get – for example – strVal, we need to simply annotate the getter:

@JsonProperty("strVal")
public String getStringValue() {
    return stringValue;
}

Now, on serialization, we will get the desired output:

{"strVal":"some value"}

A simple unit test should verify the output is correct:

@Test
public void givenNameOfFieldIsChanged_whenSerializing_thenCorrect() 
  throws JsonParseException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged();
    dtoObject.setStringValue("a");

    String dtoAsString = mapper.writeValueAsString(dtoObject);

    assertThat(dtoAsString, not(containsString("stringValue")));
    assertThat(dtoAsString, containsString("strVal"));
}

3. Conclusion

Marshaling an entity to adhere to a specific JSON format is a common task – and this article shows how to do is simply by using the @JsonProperty annotation.

The implementation of all these examples and code snippets can be found in my github project.

Related posts:

Quick Guide to Spring Bean Scopes
Introduction to Spring Cloud OpenFeign
Request Method Not Supported (405) in Spring
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
HashMap trong Java hoạt động như thế nào?
Spring Boot - Enabling HTTPS
Properties with Spring and Spring Boot
Apache Commons Collections MapUtils
Java Program to Implement SimpeBindings API
Java Program to Find the GCD and LCM of two Numbers
The Spring @Controller and @RestController Annotations
Assertions in JUnit 4 and JUnit 5
Java Program to Implement Shunting Yard Algorithm
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
New Features in Java 10
Phương thức tham chiếu trong Java 8 – Method References
Java Program to find the peak element of an array using Binary Search approach
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
A Guide to the ViewResolver in Spring MVC
Introduction to Spring Cloud Rest Client with Netflix Ribbon
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Lloyd’s Algorithm
Debugging Reactive Streams in Java
Guide to the Synchronized Keyword in Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Overview of the java.util.concurrent
Handling Errors in Spring WebFlux
ETL with Spring Cloud Data Flow
Introduction to Thread Pools in Java
How to Replace Many if Statements in Java