[Jackson API Examples] - How to Change Field Name in JSON String using @JsonProperty annotation

#JavaInspires

 Hi Guys,

Welcome to Java Inspires.



In this post, we will see how to change field name in JSON String using @JsonProperty annotation on serialization of Java object.

Without @JsonAnnotation


package com.javainspires;

import java.io.IOException;
import java.util.Date;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * 
 * @author #JavaInspires
 *
 */
public class MainApp {

	public static void main(String[] args) throws IOException {

		// create employee object
		Employee employee = new Employee();
		employee.setId(11111L);
		employee.setFirstName("Musk");
		employee.setLastName("Elon");
		employee.setDoj(new Date());

		// create ObjectMapper object
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		String jsonString = mapper.writeValueAsString(employee);

		// print json string
		System.out.println(jsonString);
	}
}

class Employee {
	Long id;
	String firstName;
	String lastName;
	Date doj;

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Employee(Long id, String firstName, String lastName, Date doj) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.doj = doj;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Date getDoj() {
		return doj;
	}

	public void setDoj(Date doj) {
		this.doj = doj;
	}

}



Output:

{
  "id" : 11111,
  "firstName" : "Musk",
  "lastName" : "Elon",
  "doj" : 1623686243246
}


With @JsonAnnotation


package com.javainspires;

import java.io.IOException;
import java.util.Date;

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

/**
 * 
 * @author #JavaInspires
 *
 */
public class MainApp {

	public static void main(String[] args) throws IOException {

		// create employee object
		Employee employee = new Employee();
		employee.setId(11111L);
		employee.setFirstName("Musk");
		employee.setLastName("Elon");
		employee.setDoj(new Date());

		// create ObjectMapper object
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		String jsonString = mapper.writeValueAsString(employee);

		// print json string
		System.out.println(jsonString);
	}
}

class Employee {
	@JsonProperty("employee_id")
	Long id;
	@JsonProperty("first_name")
	String firstName;
	@JsonProperty("last_name")
	String lastName;
	@JsonProperty("date_of_joining")
	Date doj;

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Employee(Long id, String firstName, String lastName, Date doj) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.doj = doj;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Date getDoj() {
		return doj;
	}

	public void setDoj(Date doj) {
		this.doj = doj;
	}

}



Output:


{
  "employee_id" : 11111,
  "first_name" : "Musk",
  "last_name" : "Elon",
  "date_of_joining" : 1623686316882
}




Post a Comment

Previous Post Next Post