Are there any Java libraries that can diff two JSON strings and generate a tree structure that you can traverse to find out what nodes were added, what nodes were deleted, and what nodes were modified?
I found two Java libraries that can diff two JSON strings and tell what was added, deleted, and modified. Both libraries generate output in accordance with RFC 6902 (JSON Patch):
zjsonpatch is better because it can detect items being inserted/removed from arrays. Fge json-patch cannot handle that - if an item is inserted into the middle of an array, it will think that item and every item after that was changed, since they are all shifted over by one.
Example usage for zjsonpatch:
ObjectMapper jackson = new ObjectMapper(); JsonNode beforeNode = jackson.readTree(beforeString); JsonNode afterNode = jackson.readTree(afterString); JsonNode patchNode = JsonDiff.asJson(beforeNode, afterNode); String diff = patchNode.toString();
I did some research to finalise a diff generation library for myself. The bug pointed out by @pacoverflow seems resolved. I did not face this issue. My vote is for fge json-patch because of following reasons:
- Good documentation support.
- Supports serialisation.
- Most closely follows RFC 6902 standards. zjsonpatch may behave different with int and float value comparison (treating 1 and 1.0 different).
- Open bugs with zjsonpatch indicate possibility of broken patches.