使用Java代码获取两个JSON字符串的差异

时间:2023-01-18 23:30:36

Can anybody suggest some Java Library or Code to get a diff of two JSON Strings?

任何人都可以建议一些Java库或代码来获得两个JSON字符串的差异?

4 个解决方案

#1


12  

I had the exact same problem and ended up writing my own library:

我有完全相同的问题,最后写了我自己的库:

https://github.com/algesten/jsondiff

https://github.com/algesten/jsondiff

It does both diffing/patching.

它同时进行差异/修补。

Diffs are JSON-objects themselves and have a simple syntax for object merge/replace and array insert/replace.

Diffs是JSON对象本身,具有对象合并/替换和数组插入/替换的简单语法。

Example:

例:

original
{
   a: { b: 42 }
}

patch
{
  "~a" { c: 43 }
}

The ~ indicates an object merge.

〜表示对象合并。

result
{
   a: { b: 42, c: 43 }
}

#2


10  

For one specific suggestion, you could use Jackson, bind JSON strings into JSON trees, and compare them for equality. Something like:

对于一个特定的建议,您可以使用Jackson,将JSON字符串绑定到JSON树中,并将它们进行相等性比较。就像是:

ObjectMapper mapper = new ObjectMapper();
JsonNode tree1 = mapper.readTree(jsonString1);
JsonNode tree2 = mapper.readTree(jsonString2);
if (tree1.equals(tree2)) { 
  // yes, contents are equal -- note, ordering of arrays matters, objects not
} else { 
  // not equal
}

equality comparison is by value and should work as expected with respect to JSON arrays, objects and primitive values.

等价比较是按值进行的,并且应该按照JSON数组,对象和原始值的预期工作。

#3


2  

Personally, I would suggest de-serializing the JSON strings back into objects and comparing the objects.

就个人而言,我建议将JSON字符串反序列化为对象并比较对象。

That way you don't have to worry about extra whitespace/formatting between the two JSON strings (two strings could be formatted wildly different and still represent equal objects).

这样您就不必担心两个JSON字符串之间的额外空格/格式(两个字符串的格式可能大不相同,仍然代表相同的对象)。

#4


1  

The above answers only checks Json against equality.If you want to get a diff look at javers library. Convert the json into java objects using Jackson and then use javers compare value objects to get the diff. Here is the sample piece code.(Assuming p1,p2 are two java objects)

上面的答案只检查Json是否相等。如果你想看看javers库的差异。使用Jackson将json转换为java对象,然后使用javers比较值对象来获取diff。这是示例代码。(假设p1,p2是两个java对象)

Javers j = JaversBuilder.javers().build();
        Diff diff = j.compare(p1, p2);
        if (diff.hasChanges()) {
            List<Change> changes = diff.getChanges();
            for (Change change : changes) {
                if (change instanceof ValueChange) {
                    ValueChange valChange = (ValueChange) change;
System.out.println(valChange.getPropertyName() + " -- " + valChange.getLeft() + "--"+valchange.getRight());
}
 }

This will give you the differences as key value and the expected and actual values.

这将为您提供关键值以及预期值和实际值之间的差异。

#1


12  

I had the exact same problem and ended up writing my own library:

我有完全相同的问题,最后写了我自己的库:

https://github.com/algesten/jsondiff

https://github.com/algesten/jsondiff

It does both diffing/patching.

它同时进行差异/修补。

Diffs are JSON-objects themselves and have a simple syntax for object merge/replace and array insert/replace.

Diffs是JSON对象本身,具有对象合并/替换和数组插入/替换的简单语法。

Example:

例:

original
{
   a: { b: 42 }
}

patch
{
  "~a" { c: 43 }
}

The ~ indicates an object merge.

〜表示对象合并。

result
{
   a: { b: 42, c: 43 }
}

#2


10  

For one specific suggestion, you could use Jackson, bind JSON strings into JSON trees, and compare them for equality. Something like:

对于一个特定的建议,您可以使用Jackson,将JSON字符串绑定到JSON树中,并将它们进行相等性比较。就像是:

ObjectMapper mapper = new ObjectMapper();
JsonNode tree1 = mapper.readTree(jsonString1);
JsonNode tree2 = mapper.readTree(jsonString2);
if (tree1.equals(tree2)) { 
  // yes, contents are equal -- note, ordering of arrays matters, objects not
} else { 
  // not equal
}

equality comparison is by value and should work as expected with respect to JSON arrays, objects and primitive values.

等价比较是按值进行的,并且应该按照JSON数组,对象和原始值的预期工作。

#3


2  

Personally, I would suggest de-serializing the JSON strings back into objects and comparing the objects.

就个人而言,我建议将JSON字符串反序列化为对象并比较对象。

That way you don't have to worry about extra whitespace/formatting between the two JSON strings (two strings could be formatted wildly different and still represent equal objects).

这样您就不必担心两个JSON字符串之间的额外空格/格式(两个字符串的格式可能大不相同,仍然代表相同的对象)。

#4


1  

The above answers only checks Json against equality.If you want to get a diff look at javers library. Convert the json into java objects using Jackson and then use javers compare value objects to get the diff. Here is the sample piece code.(Assuming p1,p2 are two java objects)

上面的答案只检查Json是否相等。如果你想看看javers库的差异。使用Jackson将json转换为java对象,然后使用javers比较值对象来获取diff。这是示例代码。(假设p1,p2是两个java对象)

Javers j = JaversBuilder.javers().build();
        Diff diff = j.compare(p1, p2);
        if (diff.hasChanges()) {
            List<Change> changes = diff.getChanges();
            for (Change change : changes) {
                if (change instanceof ValueChange) {
                    ValueChange valChange = (ValueChange) change;
System.out.println(valChange.getPropertyName() + " -- " + valChange.getLeft() + "--"+valchange.getRight());
}
 }

This will give you the differences as key value and the expected and actual values.

这将为您提供关键值以及预期值和实际值之间的差异。