This post explains how to sort fields in a JSON object using the @JsonPropertyOrder annotation provided by Jackson library. You can sort either by a specific order or by alphabetic order.

You know, in REST API development with FasterXML/jackson - the Java JSON library - the order of the fields in a JSON object is not like the order declared in the entity/DTO class. Say, we have the following DTO class:

public class LocationDTO {

	private String code;
	
	@JsonProperty("city_name")
	private String cityName;
	
	@JsonProperty("region_name")
	private String regionName;

	@JsonProperty("country_name")
	private String countryName;

	@JsonProperty("country_code")
	private String countryCode;
	
	private boolean enabled;
	
	// getters and setters...
	
}

You can see the fields in this class is declared in this order: code, city_name, region_name, country_name, country_code and enabled. But the API GET /v1/locations/{code} may return a JSON object as follows:

{
    "code": "MBMHR_IN",
    "enabled": true,
    "city_name": "Mumbai",
    "region_name": "Maharashtra",
    "country_name": "India",
    "country_code": "IN"
}

So how to sort the fields in this JSON object in a specific order? With FasterXML/Jackson library, you can use the @JsonPropertyOrder. For example:

@JsonPropertyOrder(
	{"code", "city_name", "region_name", "country_code", "country_name", "enabled"}
	)
public class LocationDTO {

	// fields...
}

You pass an array of String specifies the field names in the order declared in the array. Then the output will be as expected:

{
    "code": "MBMHR_IN",
    "city_name": "Mumbai",
    "region_name": "Maharashtra",
    "country_code": "IN",
    "country_name": "India",
    "enabled": true
}

Note that using the @JsonPropertyOrder annotation, you don’t have to specify all the field names. That means you can just specify order of some fields that should be appeared first, and the order of the remaining fields is not set. For example:

@JsonPropertyOrder({"code", "city_name"})
public class LocationDTO {

	// fields....
}

This ensures that the field code will come first, then followed by city_name. And the remaining fields can be in unspecified order. The output maybe as below:

{
    "code": "MBMHR_IN",
    "city_name": "Mumbai",
    "enabled": true,
    "region_name": "Maharashtra",
    "country_name": "India",
    "country_code": "IN"
}

If you want to specify the fields sorted by alphabetic order, use the @JsonPropertyOrder annotation as follows:

@JsonPropertyOrder(alphabetic = true)
public class LocationDTO {

	// fields....
}

The response body will be like this:

{
    "code": "MBMHR_IN",
    "city_name": "Mumbai",
    "enabled": true,
    "region_name": "Maharashtra",
    "country_name": "India",
    "country_code": "IN"
}

You see, the fields in this JSON object are sorted alphabetically.

Those are some examples of sorting fields in a JSON object with Jackson - the Java JSON library. I hope you found this post helpful. To see the coding in action, watch the following video:

 

Reference:

Annotation Type JsonPropertyOrder (Jackson Docs)

 

Related REST API Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.