Can anybody suggest some Java Library or Code to get a diff of two JSON Strings?
-
What do you want out of this comparison? What's wrong with just comparing the strings with String.equals? – Paul Butcher Jul 09 '10 at 15:31
-
What do you mean by compare? If they're just strings, what's JSON have to do with it? – Matt Ball Jul 09 '10 at 15:32
-
1As per the comment on Justin's answer he actually want to have a diff, not to compare them on equality. – BalusC Jul 09 '10 at 23:18
5 Answers
I had the exact same problem and ended up writing my own library:
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.
Example:
original
{
a: { b: 42 }
}
patch
{
"~a" { c: 43 }
}
The ~
indicates an object merge.
result
{
a: { b: 42, c: 43 }
}

- 13,052
- 4
- 54
- 77
For one specific suggestion, you could use Jackson, bind JSON strings into JSON trees, and compare them for equality. Something like:
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.
-
11However this just tells you whether the JSON is different or not; it doesn't give any hint about what those differences are (diff/patch) if they happen to be different. – andresp Dec 08 '13 at 19:26
-
1True, although you can then recursively descend and see where they differ, Merkle-tree style. – StaxMan Jul 10 '15 at 22:32
-
@gaurav example given works the way explained in 2.9. Is that what you are asking? – StaxMan Feb 14 '19 at 17:11
-
I haven't tried it though but am skeptical if it will be correct if one of the json is having a different order when doing a equals between the tress. – Sid Mar 30 '19 at 12:05
Personally, I would suggest de-serializing the JSON strings back into objects and comparing the objects.
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).

- 242,243
- 40
- 408
- 536
-
Yes this is an important point, if the 2 JSON strings contain the same data but with the properties in a different order (for example), should they be considered equal? – JonoW Jul 09 '10 at 15:39
-
1i am just curious if he can canonicalize the json string and then compare both the strings? – Aravind Yarram Jul 09 '10 at 15:40
-
By comparison, I mean - to identify such keys where values are differing. Basically need to identify all keys with different values . Example: String 1 : {"format":"example","content":"test", "no_diff":"no_diff"} String 2 : {"format":"example1","content":"test1", "no_diff":"no_diff"} On comparison, it should return - format and content keys with their values. – Prafull N Jul 09 '10 at 15:41
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)
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.

- 121
- 6
Accepted answer is good and work is splendid.
Still, for a more maintained library, I would suggest https://github.com/java-json-tools/json-patch, which I just used to solve JSON masking (see Apply a mask on a JSON to keep only mandatory data in case you are curious).
Note: the usage of your own
ObjectMapper
injson-patch
library makes it a bit more practical, for it avoids reading and rewriting with your customizedObjectMapper
in order to have a valid JSON according to your specific standards.

- 607
- 5
- 18