jsondelta-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jsondelta.py +118 -0
- jsondelta_cli-0.1.0.dist-info/METADATA +39 -0
- jsondelta_cli-0.1.0.dist-info/RECORD +7 -0
- jsondelta_cli-0.1.0.dist-info/WHEEL +5 -0
- jsondelta_cli-0.1.0.dist-info/entry_points.txt +2 -0
- jsondelta_cli-0.1.0.dist-info/licenses/LICENSE +9 -0
- jsondelta_cli-0.1.0.dist-info/top_level.txt +1 -0
jsondelta.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""jsondelta: semantic diff of two JSON files (key order ignored, paths reported).
|
|
3
|
+
|
|
4
|
+
Exit codes: 0 identical, 1 differences found, 2 bad input.
|
|
5
|
+
"""
|
|
6
|
+
import argparse
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _path(base, key):
|
|
14
|
+
if isinstance(key, int):
|
|
15
|
+
return "%s[%d]" % (base, key)
|
|
16
|
+
return "%s.%s" % (base, key) if base else str(key)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _canon(v):
|
|
20
|
+
return json.dumps(v, sort_keys=True, separators=(",", ":"))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def diff(a, b, path="", unordered=False, ignore=()):
|
|
24
|
+
"""Yield (op, path, old, new); op is '+', '-' or '~'."""
|
|
25
|
+
if path in ignore:
|
|
26
|
+
return
|
|
27
|
+
if isinstance(a, dict) and isinstance(b, dict):
|
|
28
|
+
for k in sorted(set(a) | set(b), key=str):
|
|
29
|
+
p = _path(path, k)
|
|
30
|
+
if k not in b:
|
|
31
|
+
if p not in ignore:
|
|
32
|
+
yield ("-", p, a[k], None)
|
|
33
|
+
elif k not in a:
|
|
34
|
+
if p not in ignore:
|
|
35
|
+
yield ("+", p, None, b[k])
|
|
36
|
+
else:
|
|
37
|
+
yield from diff(a[k], b[k], p, unordered, ignore)
|
|
38
|
+
elif isinstance(a, list) and isinstance(b, list):
|
|
39
|
+
if unordered:
|
|
40
|
+
rest = [_canon(x) for x in b]
|
|
41
|
+
bmap = list(b)
|
|
42
|
+
for i, x in enumerate(a):
|
|
43
|
+
c = _canon(x)
|
|
44
|
+
if c in rest:
|
|
45
|
+
j = rest.index(c)
|
|
46
|
+
del rest[j], bmap[j]
|
|
47
|
+
else:
|
|
48
|
+
yield ("-", _path(path, i), x, None)
|
|
49
|
+
for y in bmap:
|
|
50
|
+
yield ("+", path + "[]", None, y)
|
|
51
|
+
else:
|
|
52
|
+
for i in range(max(len(a), len(b))):
|
|
53
|
+
p = _path(path, i)
|
|
54
|
+
if i >= len(b):
|
|
55
|
+
yield ("-", p, a[i], None)
|
|
56
|
+
elif i >= len(a):
|
|
57
|
+
yield ("+", p, None, b[i])
|
|
58
|
+
else:
|
|
59
|
+
yield from diff(a[i], b[i], p, unordered, ignore)
|
|
60
|
+
else:
|
|
61
|
+
if type(a) is not type(b) or a != b:
|
|
62
|
+
yield ("~", path or "$", a, b)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _fmt(v):
|
|
66
|
+
return json.dumps(v, sort_keys=True, ensure_ascii=False)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def render(changes):
|
|
70
|
+
out = []
|
|
71
|
+
for op, p, old, new in changes:
|
|
72
|
+
p = p or "$"
|
|
73
|
+
if op == "-":
|
|
74
|
+
out.append("- %s: %s" % (p, _fmt(old)))
|
|
75
|
+
elif op == "+":
|
|
76
|
+
out.append("+ %s: %s" % (p, _fmt(new)))
|
|
77
|
+
else:
|
|
78
|
+
out.append("~ %s: %s -> %s" % (p, _fmt(old), _fmt(new)))
|
|
79
|
+
return out
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _load(name):
|
|
83
|
+
try:
|
|
84
|
+
with (sys.stdin if name == "-" else open(name, encoding="utf-8")) as f:
|
|
85
|
+
return json.load(f)
|
|
86
|
+
except OSError as e:
|
|
87
|
+
sys.exit("jsondelta: cannot read %s: %s" % (name, e.strerror))
|
|
88
|
+
except ValueError as e:
|
|
89
|
+
sys.exit("jsondelta: invalid JSON in %s: %s" % (name, e))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def main(argv=None):
|
|
93
|
+
ap = argparse.ArgumentParser(prog="jsondelta", description=__doc__.split("\n")[0])
|
|
94
|
+
ap.add_argument("old")
|
|
95
|
+
ap.add_argument("new")
|
|
96
|
+
ap.add_argument("--unordered", action="store_true", help="treat arrays as multisets")
|
|
97
|
+
ap.add_argument("--ignore", action="append", default=[], metavar="PATH",
|
|
98
|
+
help="skip this path, e.g. meta.updated (repeatable)")
|
|
99
|
+
ap.add_argument("--json", action="store_true", help="machine-readable output")
|
|
100
|
+
ap.add_argument("-q", "--quiet", action="store_true", help="no output, exit code only")
|
|
101
|
+
ap.add_argument("--version", action="version", version=__version__)
|
|
102
|
+
args = ap.parse_args(argv)
|
|
103
|
+
if args.old == "-" and args.new == "-":
|
|
104
|
+
sys.exit("jsondelta: only one input can be '-'")
|
|
105
|
+
a, b = _load(args.old), _load(args.new)
|
|
106
|
+
changes = list(diff(a, b, "", args.unordered, set(args.ignore)))
|
|
107
|
+
if not args.quiet:
|
|
108
|
+
if args.json:
|
|
109
|
+
print(json.dumps([{"op": o, "path": p or "$", "old": x, "new": y}
|
|
110
|
+
for o, p, x, y in changes], ensure_ascii=False, indent=2))
|
|
111
|
+
else:
|
|
112
|
+
for line in render(changes):
|
|
113
|
+
print(line)
|
|
114
|
+
return 1 if changes else 0
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
if __name__ == "__main__":
|
|
118
|
+
sys.exit(main())
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jsondelta-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Semantic JSON diff: ignores key order, reports paths, optional unordered arrays. Stdlib only.
|
|
5
|
+
Author-email: agent-one <rafal.baranowski.iot@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/rafal-one/jsondelta
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# jsondelta
|
|
16
|
+
|
|
17
|
+
Semantic diff of two JSON files. Key order is ignored, changes are reported by path, and
|
|
18
|
+
exit codes make it CI-friendly. Python 3.9+, stdlib only.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
$ jsondelta old.json new.json
|
|
22
|
+
~ server.port: 80 -> 8080
|
|
23
|
+
- features[2]: "beta"
|
|
24
|
+
+ owner: "ops"
|
|
25
|
+
$ jsondelta a.json b.json --unordered --ignore meta.updated
|
|
26
|
+
$ cat new.json | jsondelta old.json - --json
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Options: `--unordered` (arrays as multisets), `--ignore PATH` (repeatable), `--json`, `-q`.
|
|
30
|
+
Exit codes: 0 identical, 1 differences, 2 bad input (invalid JSON, unreadable file).
|
|
31
|
+
Types matter: `true` vs `1` is a change.
|
|
32
|
+
|
|
33
|
+
Install: `pip install jsondelta-cli` (command: `jsondelta`) or copy `jsondelta.py`.
|
|
34
|
+
|
|
35
|
+
## Tests
|
|
36
|
+
`pytest` (11 tests; Python 3.9 and 3.14).
|
|
37
|
+
|
|
38
|
+
## Support
|
|
39
|
+
If this saves you time, you can donate via [PayPal](https://paypal.me/RafalBaranowski). MIT license.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
jsondelta.py,sha256=lZeAuKxeKP2Sp8vmFGSMRSvTMP0nGGhLPjvckMyg6KM,4000
|
|
2
|
+
jsondelta_cli-0.1.0.dist-info/licenses/LICENSE,sha256=-hZriFiksFsGGM2WbhdnQzoXUOFuMGve5P3y4pvA0Ws,1066
|
|
3
|
+
jsondelta_cli-0.1.0.dist-info/METADATA,sha256=-ykpLPVDWvIxADjSTeLJe7zBj0gE_phy4dR8m_wIWNU,1335
|
|
4
|
+
jsondelta_cli-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
5
|
+
jsondelta_cli-0.1.0.dist-info/entry_points.txt,sha256=zIr0EGdTAY2jWDDVx0R1tgdTaFkpLM8Qu492mAwdsrE,45
|
|
6
|
+
jsondelta_cli-0.1.0.dist-info/top_level.txt,sha256=xf01i2vZgCLoZRSxZBVMTo31wLM19Oeg9DTIsILu9Bg,10
|
|
7
|
+
jsondelta_cli-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 agent-one
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
jsondelta
|