Friday, 10 March 2023

Jackson: sort list of objects (JsonNode, ArrayNode)

Suppose, I have employee details like below

 

emps.json

{
	"emps" : [
		{
			"id" : 1,
			"name" : "Krishna",
			"age" : 34
		},
		{
			"id" : 2,
			"name" : "Gopi",
			"age" : 39
		},
		{
			"id" : 3,
			"name" : "Ram",
			"age" : 24
		},
		{
			"id" : 4,
			"name" : "Arun",
			"age" : 34
		}
	]
}

 

Sort the data by age

Step 1: Get the JsonNode from given json.

ObjectMapper objectMapper = new ObjectMapper();
String empsInfo = resourceAsString("emps.json");
JsonNode jsonNode = objectMapper.readTree(empsInfo);
JsonNode empsNode = jsonNode.get("emps");

 

Step 2: Sort the empsNode by age.

List<JsonNode> dataNodes = empsNode.findParents("age");
List<JsonNode> nodesByAge = dataNodes.stream().sorted(Comparator.comparing(o -> o.get("age").asInt())).collect(Collectors.toList());

Find the below working application.

 

App.java

package com.sample.app;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

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

public class App {

	public static String resourceAsString(String resourceName) throws IOException {
		ClassLoader classLoader = App.class.getClassLoader();
		URL url = classLoader.getResource(resourceName);
		if (url == null) {
			return null;
		}

		URLConnection urlConnection = url.openConnection();

		urlConnection.setUseCaches(false);

		try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
			char[] buffer = new char[1048];
			StringBuilder builder = new StringBuilder();

			int count = -1;
			while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
				builder.append(buffer, 0, count);
			}

			return builder.toString();
		}

	}

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

		ObjectMapper objectMapper = new ObjectMapper();
		String empsInfo = resourceAsString("emps.json");
		JsonNode jsonNode = objectMapper.readTree(empsInfo);

		JsonNode empsNode = jsonNode.get("emps");
		List<JsonNode> dataNodes = empsNode.findParents("age");
		List<JsonNode> nodesByAge = dataNodes.stream().sorted(Comparator.comparing(o -> o.get("age").asInt()))
				.collect(Collectors.toList());

		System.out.println("Sort by age");
		for (JsonNode emp : nodesByAge) {
			System.out.println(emp);
		}

		System.out.println("\nSort by name");
		dataNodes = empsNode.findParents("name");
		List<JsonNode> nodesByName = dataNodes.stream().sorted(Comparator.comparing(o -> o.get("name").asText()))
				.collect(Collectors.toList());

		for (JsonNode emp : nodesByName) {
			System.out.println(emp);
		}
	}

}

Output

Sort by age
{"id":3,"name":"Ram","age":24}
{"id":1,"name":"Krishna","age":34}
{"id":4,"name":"Arun","age":34}
{"id":2,"name":"Gopi","age":39}

Sort by name
{"id":4,"name":"Arun","age":34}
{"id":2,"name":"Gopi","age":39}
{"id":1,"name":"Krishna","age":34}
{"id":3,"name":"Ram","age":24}




Previous                                                 Next                                                 Home

No comments:

Post a Comment