Reading JSON Documents as Maps and Comparing Them – 以地图形式读取 JSON 文档并进行比较

最后修改: 2023年 8月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

在本教程中,我们将了解以 Maps 形式读取 JSON 文档并对其进行比较的不同方法。我们还将了解查找两个 Map 之间差异的方法。

2. Converting to Map

2.转换为 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.

首先,我们将了解将 JSON 文档转换为 Maps 的不同方法。让我们来看看我们将用于测试的 JSON 对象。

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

让我们创建一个名为 first.json 的文件,内容如下:

{
  "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:

同样,让我们创建另一个名为 second.json 的文件,内容如下:

{
  "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:

我们可以看到,上述 JSON 文档有两处不同:

  • 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

2.1.利用Jackson

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

Jackson 是一个用于 JSON 操作的流行库。我们可以使用 Jackson 将 JSON 转换为 Map.

Let’s start by adding the Jackson dependency:

首先,让我们添加 Jackson 依赖项

<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:

现在,我们可以使用 Jackson 将 JSON 文档转换为 Map 文件:

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.

在这里,我们使用 ObjectMapper 类中的 readValue() 方法将 JSON 文档转换为 Map 对象。该方法将 JSON 文档作为一个 File 对象和一个 TypeReference 对象作为参数。

2.2. Using Gson

2.2.使用 Gson

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

同样,我们也可以使用 Gson 将 JSON 文档转换为 Map。为此,我们需要包含 依赖关系

<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:

现在让我们看看转换 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.

在这里,我们使用 Gson 类中的 fromJson() 方法将 JSON 文档转换为 Map 。该方法将 JSON 文档作为一个 FileReader 对象和一个 TypeToken 对象作为参数。

3. Comparing Maps

3.比较 Maps

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

现在,我们已经将 JSON 文档转换为 Maps 文件,让我们来看看比较它们的不同方法。

3.1. Using Guava’s Map.difference()

3.1.使用 Guava 的 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:

Guava 提供了一个 Maps.difference() 方法,可用于比较两个 Maps 。要使用该方法,让我们在项目中添加 Guava 依赖关系:

<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:

现在,让我们看看比较 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.

Guava 只能比较一个级别的 Maps 。这不适用于我们的情况,因为我们有一个嵌套的 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:

让我们看看如何比较上面嵌套的 Maps 。我们使用 entriesDiffering() 方法来获取 Map 之间的差异。该方法将返回一个 Map 差异,其中键是指向值的路径,值是一个 MapDifference.ValueDifference 对象。该对象包含两个 Map 中的值。如果运行测试,我们将看到 Maps 之间不同的键及其值:

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.

我们可以看到,这表明 汽车、地址 子女 字段是不同的,并且列出了这些差异。但是,这并没有显示是哪些嵌套字段导致了这些差异。例如,它没有指出 address 对象中的 street 字段是不同的。

3.2. Flattening Maps

3.2 扁平化 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.

为了精确指出嵌套 Map 之间的差异,我们将对 Map 进行扁平化处理,使每个键都是指向值的路径。例如,address 对象中的 street 键将被扁平化为 address.street 等等。

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:

在这里,我们使用递归来扁平化 Map 。对于任何字段,以下条件之一将为真:

  • 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.

这将是一个递归过程,直到我们到达 Map 的最后一级。此时,我们将得到一个扁平化的 Map ,每个键都是通向值的路径。

3.3. Testing

3.3.测试

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

现在我们已经将 Maps 扁平化,让我们看看如何使用 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

4.结论

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.

在本文中,我们将比较 Java 中的两个 JSON 文档。我们研究了将 JSON 文档转换为 Maps 的不同方法,然后使用 Guava 的 Maps.difference() 方法对它们进行了比较。我们还研究了如何将 Maps 扁平化,以便比较嵌套的 Maps 。

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

与往常一样,本文的代码可在 GitHub 上获取。