json-delta is a set of command-line tools and multi-language libraries for diffing and patching JSON files. The tools includes:
- json_cat: concatenates the JSON file
- json_diff: outputs diff of two JSON files
- json_patch: applies patch from json_diff onto a JSON file
The supported languages include Python 2.7+ and 3+, JavaScript, Racket, and Perl. For languages other than Python, you need to get from its repository.
Consider the following two files, foo and bar:
{"foods": ["spam", "spam", "spam", "spam"], "weaponry": "Mainly battleaxes.", "spanish inquisition expected": false, "drinks": "Delicious mead!", "other supplies": null}
{"foods": ["spam", "spam", "spam", "pickled eggs", "spam"], "weaponry": "Mainly battleaxes.", "spanish inquisition expected": false, "drinks": "Soda water."}
Now, diff with json_diff:
$ json_diff foo bar Size of delta 67.808% size of original (original: 146 chars, delta: 99 chars)
[[["other supplies"]],[["drinks"],"Soda water."],[["foods",3],"pickled eggs"],[["foods",4],"spam"]]
Using the unified option -u:
$ json_diff -u foo bar Size of delta 67.808% size of original (original: 146 chars, delta: 99 chars) --- foo 2014-04-29 14:04:49 CST +++ bar 2014-04-29 14:04:11 CST { "weaponry": "Mainly battleaxes.", ..., "foods": [ "spam", ...(1), "spam", - "spam" + "pickled eggs", + "spam" ], - "other supplies": null, "drinks": - "Delicious mead!" + "Soda water." }
To patch the foo and output to standard output:
$ json_patch foo <(json_diff foo bar) Size of delta 67.808% size of original (original: 146 chars, delta: 99 chars)
{"foods": ["spam", "spam", "spam", "pickled eggs", "spam"], "weaponry": "Mainly battleaxes.", "spanish inquisition expected": false, "drinks": "Soda water."} /tmp $
By default, it’s not in-place patching, but you can use --output to overwrite the original file or any other file.
json-delta is written by Philip J. Roberts under the Simplified BSD License (2-clause), currently version 0.1 (2014-04-15).
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.