Skip to content

Instantly share code, notes, and snippets.

@blalor
Last active January 7, 2020 15:01
Show Gist options
  • Save blalor/2af102c7e661e5c897f0a0652fd300ca to your computer and use it in GitHub Desktop.
Save blalor/2af102c7e661e5c897f0a0652fd300ca to your computer and use it in GitHub Desktop.
Diff for JSON data

jsondiff

Requires datadiff.

Also useful with yaml2json.

example

[:~] $ jsondiff <( echo '{"foo":"bar"}' ) <( echo '{"foo":"bar", "baz":"bap"}' )
--- a
+++ b
{
+u'baz': u'bap',
 u'foo': u'bar',
}
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
## https://pypi.python.org/pypi/datadiff
import datadiff
import json
def main(src_a, src_b, depth=9):
with open(src_a) as ifp:
obj_a = json.load(ifp)
with open(src_b) as ifp:
obj_b = json.load(ifp)
if obj_a != obj_b:
print datadiff.diff(obj_a, obj_b)
if __name__ == "__main__":
import sys
main(*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment