讀取 JSON 文檔作為映射並進行比較

Jackson
Remote
0
08:00 PM · Nov 30 ,2025

1. 概述

在本教程中,我們將探討如何以 Map 格式讀取 JSON 文檔,並進行比較。 此外,我們還將研究如何查找兩個 Map 之間的差異。

2. Converting to Map

首先,我們將 JSON 文檔轉換為 Map。讓我們看看用於測試的不同方法。

讓我們創建一個名為 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
    }
  ]
}

同樣,讓我們創建一個名為 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
    }
  ]
}

正如我們所見,上述 JSON 文檔之間存在兩個差異:

  • “cars” 數組的值不同
  • “street” 鍵在“address” 對象中的值不同
  • “children” 數組存在多個差異

2.1. Using Jackson

Jackson 是用於 JSON 操作的流行庫。我們可以使用 Jackson 將 JSON 轉換為 Map

讓我們首先添加 Jackson 依賴

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

現在,我們可以使用 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>>() {});
    }
}

這裏,我們使用 `readValue()` 方法來自 `ObjectMapper` 類來將 JSON 文檔轉換為 Map。它接受 JSON 文檔作為 `File` 對象和 `TypeReference` 對象作為參數。

2.2. Using Gson

同樣,我們可以使用 Gson 將 JSON 文檔轉換為 Map。我們需要包含用於此目的的 依賴

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

現在,讓我們查看將 JSON 轉換為 Map 的代碼:

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());
}

這裏,我們使用 `fromJson()` 方法來自 `Gson` 類的代碼將 JSON 文檔轉換為 Map。它接受 JSON 文檔作為 `FileReader` 對象和 `TypeToken` 對象作為參數。

3. Comparing Maps

Now that we’ve converted the JSON documents to Maps,

3.1. Using Guava’s Map.difference()

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


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

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

@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();
}

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 thecars, address, andchildren fields are different, and the differences are listed. However, this doesn’t point out which nested fields are leading to these differences. For example, it doesn’t point out that thestreet field in theaddress 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, thestreet key in theaddress object will be flattened toaddress.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, theaddress object will be flattened toaddress.street andaddress.city.
  • Next, we can check if the value is aList (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 theflatten() method with each object as the parameter. For example, thechildren array will be flattened tochildren[0].name, children[0].age, children[1].name and children[1].age.
  • If the value is neither aMap nor aList, 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 usingMaps.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. 結論

在本文中,我們探討了如何在Java中比較兩個JSON文檔。我們研究了將JSON文檔轉換為Map的不同方法,然後使用Guava的Maps.difference()方法進行比較。我們還研究瞭如何扁平化Map以進行嵌套Map的比較。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.