Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll look at different ways to read JSON documents as Maps and compare them. We’ll also look at ways to find the differences between the two Maps.

2. Converting to Map

First, we’ll look at different ways to convert JSON documents to Maps. Let’s look at the JSON objects we’ll use for our test.

Let’s create a file named first.json with the following content:

{
  "name": "John",
  "age": 30,
  "cars": [
    "Ford",
    "BMW"
  ],
  "address": {
    "street": "Second Street",
    "city": "New York"
  },
  "children": [
    {
      "name": "Sara",
      "age": 5
    },
    {
      "name": "Alex",
      "age": 3
    }
  ]
}

Similarly, let’s create another file named second.json with the following content:

{
  "name": "John",
  "age": 30,
  "cars": [
    "Ford",
    "Audi"
  ],
  "address": {
    "street": "Main Street",
    "city": "New York"
  },
  "children": [
    {
      "name": "Peter",
      "age": 5
    },
    {
      "name": "Cathy",
      "age": 10
    }
  ]
}

As we can see, there are two differences between the above JSON documents:

  • The value of the cars array is different
  • The value of the street key in the address object is different
  • The children arrays have multiple differences

2.1. Using Jackson

Jackson is a popular library used for JSON operations. We can use Jackson to convert a JSON to a Map.

Let’s start by adding the Jackson dependency:

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

Now we can convert a JSON document to Map using Jackson:

class JsonUtils {
    public static Map<String, Object> jsonFileToMap(String path) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(new File(path), new TypeReference<Map<String, Object>>() {});
    }
}

Here, we’re using the readValue() method from the ObjectMapper class to convert the JSON document to a Map. It takes the JSON document as a File object and a TypeReference object as parameters.

2.2. Using Gson

Similarly, we can also use Gson to convert the JSON document to a Map. We need to include the dependency for this:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>

Now let’s look at the code to convert the JSON:

public static Map<String, Object> jsonFileToMapGson(String path) throws IOException {
    Gson gson = new Gson();
    return gson.fromJson(new FileReader(path), new TypeToken<Map<String, Object>>() {}.getType());
}

Here, we’re using the fromJson() method from the Gson class to convert the JSON document to a Map. It takes the JSON document as a FileReader object and a TypeToken object as parameters.

3. Comparing Maps

Now that we’ve converted the JSON documents to Maps, let’s look at different ways to compare them.

3.1. Using Guava’s Map.difference()

Guava provides a Maps.difference() method that can be used to compare two Maps. To utilize this, let’s add the Guava dependency to our project:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>33.0.0-jre</version>
</dependency>

Now, let’s look at the code to compare the Maps:

@Test
void givenTwoJsonFiles_whenCompared_thenTheyAreDifferent() throws IOException {
    Map<String, Object> firstMap = JsonUtils.jsonFileToMap("src/test/resources/first.json");
    Map<String, Object> secondMap = JsonUtils.jsonFileToMap("src/test/resources/second.json");

    MapDifference<String, Object> difference = Maps.difference(firstFlatMap, secondFlatMap);
    difference.entriesDiffering().forEach((key, value) -> {
        System.out.println(key + ": " + value.leftValue() + " - " + value.rightValue());
    });
    assertThat(difference.areEqual()).isFalse();
}

Guava can only compare one level of Maps. This doesn’t work for our case as we have a nested Map.

Let’s look at how we compare our nested Maps above. We’re using the entriesDiffering() method to get the differences between the Maps. This returns a Map of differences where the key is the path to the value, and the value is a MapDifference.ValueDifference object. This object contains the values from both the Maps. If we run the test, we’ll see the keys that are different between the Maps and their values:

cars: [Ford, BMW] - [Ford, Audi]
address: {street=Second Street, city=New York} - {street=Main Street, city=New York}
children: [{name=Sara, age=5}, {name=Alex, age=3}] - [{name=Peter, age=5}, {name=Cathy, age=10}]

As we can see, this shows that the cars, address, and children fields are different, and the differences are listed. However, this doesn’t show which nested fields are leading to these differences. For example, it doesn’t point out that the street field in the address objects is different.

3.2. Flattening Maps

To precisely point out differences between nested Maps, we’ll flatten the Maps so each key is a path to the value. For example, the street key in the address object will be flattened to address.street and so on.

Let’s look at the code for this:

class FlattenUtils {
    public static Map<String, Object> flatten(Map<String, Object> map) {
        return flatten(map, null);
    }

    private static Map<String, Object> flatten(Map<String, Object> map, String prefix) {
        Map<String, Object> flatMap = new HashMap<>();
        map.forEach((key, value) -> {
            String newKey = prefix != null ? prefix + "." + key : key;
            if (value instanceof Map) {
                flatMap.putAll(flatten((Map<String, Object>) value, newKey));
            } else if (value instanceof List) {
                // check for list of primitives
                Object element = ((List<?>) value).get(0);
                if (element instanceof String || element instanceof Number || element instanceof Boolean) {
                    flatMap.put(newKey, value);
                } else {
                    // check for list of objects
                    List<Map<String, Object>> list = (List<Map<String, Object>>) value;
                    for (int i = 0; i < list.size(); i++) {
                        flatMap.putAll(flatten(list.get(i), newKey + "[" + i + "]"));
                    }
                }
            } else {
                flatMap.put(newKey, value);
            }
        });
        return flatMap;
    }
}

Here, we’re using recursion to flatten the Map. For any field, one of the following conditions will be true:

  • The value could be a Map (nested JSON object). In this case, we’ll recursively call the flatten() method with the value as the parameter. For example, the address object will be flattened to address.street and address.city.
  • Next, we can check if the value is a List (JSON array). If the list contains primitive values, we’ll add the key and value to the flattened Map.
  • If the list contains objects, we’ll recursively call the flatten() method with each object as the parameter. For example, the children array will be flattened to children[0].namechildren[0].agechildren[1].name, and children[1].age.
  • If the value is neither a Map nor a List, we’ll add the key and value to the flattened Map.

This will be recursive until we reach the last level of the Map. At this point, we’ll have a flattened Map with each key as a path to the value.

3.3. Testing

Now that we’ve flattened the Maps, let’s look at how we can compare them using Maps.difference():

@Test
void givenTwoJsonFiles_whenCompared_thenTheyAreDifferent() throws IOException {
    Map<String, Object> firstFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/first.json"));
    Map<String, Object> secondFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/second.json"));

    MapDifference<String, Object> difference = Maps.difference(firstFlatMap, secondFlatMap);
    difference.entriesDiffering().forEach((key, value) -> {
        System.out.println(key + ": " + value.leftValue() + " - " + value.rightValue());
    });
    assertThat(difference.areEqual()).isFalse();
}

Again, we’ll print the keys and values that are different. This leads to the output below:

cars: [Ford, BMW] - [Ford, Audi]
children[1].age: 3 - 10
children[1].name: Alex - Cathy
address.street: Second Street - Main Street
children[0].name: Sara - Peter

4. Conclusion

In this article, we looked at comparing two JSON documents in Java. We looked at different ways to convert the JSON documents to Maps and then compared them using Guava’s Maps.difference() method. We also looked at how we can flatten the Maps so that we can compare nested Maps.

As always, the code for this article is available over on GitHub.

Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Jackson (eBook) (cat=Jackson)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.