Technical note · python / api / test
Comparing API responses with Python: missing fields, null, and type differences
A fully offline comparison example that preserves field presence and type differences in small JSON responses.
A line diff does not always explain what changed in a JSON document. Object keys may have moved, while values such as false and 0 belong to different types. JSON Response Diff is an offline Python CLI that compares two saved UTF-8 JSON files from left to right.
It does not download responses or call APIs. It reads existing local files without changing them, and its runtime and automated tests use only the Python standard library. Every example here is synthetic.
Source code and examples on GitHub. Download the ZIP package.
Run a comparison
From the project directory:
python3 json_response_diff.py examples/before.json examples/after.jsonThe left file is the baseline and the right file is the candidate. ADDED means present only on the right; REMOVED means present only on the left; CHANGED means the value or type differs. Selected lines from the actual bundled example output:
Direction: left -> right
Excluded pointers: []
DIFFERENT: 10 difference(s) (exit 1).CHANGED "/active"
left: boolean false
right: integer 0REMOVED "/obsolete"
left: null nullMissing is not null. In the last example, the field exists with a null value on the left and is entirely absent on the right. That is a removal.
Comparison rules
Object key order is ignored. Arrays are compared by index: ["alpha","beta"] differs from ["beta","alpha"]. Nested changes use JSON Pointer paths. In a key, / becomes ~1 and ~ becomes ~0:
CHANGED "/meta/a~1b/~0key"
left: string "old"
right: string "new"This addresses the ~key field inside the a/b key under meta. The root pointer is the empty string; / addresses an empty key.
Boolean false differs from 0, true differs from 1, and integer 1 differs from decimal 1.0. Within the decimal/exponent category, 1.0, 1.00, and 1e0 are equal. Exact Decimal comparison avoids binary float rounding; there is no tolerance. Signed zero equals zero within its number category. Strings are compared exactly, without Unicode normalization.
An entirely added, removed, or type-changed container produces one difference at its own pointer. Its report contains a type-and-size summary rather than repeating every descendant. The report is not a JSON patch.
Exclusions are explicit
No field is automatically excluded, including owner, tenant, id, or request_id. Select a full JSON Pointer explicitly:
python3 json_response_diff.py examples/before.json examples/after.json \
--exclude /request_idThis leaves 9 differences in the example; the /owner change remains visible. /id does not exclude /nested/id. /* is a literal pointer, not a wildcard. Selecting a container skips its entire value. Selecting /a/x does not hide removal of the whole /a object. Explicit --exclude '' can skip the root value; all exclusions are listed in the report. Exclusions are not redaction and cannot make invalid input acceptable.
Machine-readable output
python3 json_response_diff.py examples/before.json examples/after.json --format jsonExit 0 means equal, 1 means different, and 2 means error or incomplete comparison. The JSON report includes direction, completion status, exclusions, counts, and difference paths. Numeric snapshot values are strings to retain precision in downstream readers:
{
"kind": "changed",
"path": "/count",
"before": {"type": "integer", "value": "1"},
"after": {"type": "decimal", "value": "1.0"}
}Text errors use stderr. In JSON mode, errors are one stdout JSON document with status: "error" and complete: false; partial differences are not returned as a successful result. Output delivery failure returns 2; a closed pipe may prevent delivery of an error report. There is no output-file option.
Strict inputs and bounded work
Duplicate object keys, NaN/Infinity, malformed UTF-8/JSON, UTF-8 BOM, unpaired surrogates, unreadable files, and non-regular inputs such as FIFOs or directories are rejected. Symlinks to regular files are accepted.
Fixed limits are 4 MiB per file, 64 container levels, 100,000 lexical tokens per input including keys, 10,000 differences, and an 8 MiB report. A number token may contain at most 128 characters, with exponent magnitude up to 10,000. There may be at most 100 exclusions, each at most 1,024 UTF-8 bytes. Exceeding a limit returns 2. These limits are not universal RAM or runtime guarantees.
Actual validation
On CachyOS with Python 3.14.8, all 35 automated tests passed with no skips. They cover the five acceptance groups, deterministic output, help and usage errors, resource boundaries, and unchanged synthetic input hashes. Eight sequential Linux resource probes completed under a 256 MiB address-space limit and a 5-second CPU limit. These are synthetic measurements. Other operating systems and Python versions were not tested.
python3 -m unittest discover -s tests -v
python3 tests/resource_probe.pyThe source, automated tests, two synthetic examples and usage documentation are published under the MIT license in the JSON-Response-Diff GitHub repository. The main README is Turkish and links to the English documentation. The verified source commit is 473d76e4edcc94e75a3297dabd9c6875a1ac0a66.
git clone https://github.com/Muhammet0-1/JSON-Response-Diff.git
cd JSON-Response-Diff
python3 json_response_diff.py examples/before.json examples/after.jsonThe example comparison returns exit 1 because it finds differences. The distribution ZIP contains the same nine public files; Instagram artwork and local work records are separate from the source package.
Using the ZIP package
Download JSON Response Diff 1.0.0. It contains the same nine public files as the GitHub source: the tool, two examples, tests, usage documentation and the MIT license. Instagram artwork and local work reports are excluded.
After extraction, open a terminal in json-response-diff-1.0.0 and run the commands above from that directory. README.md links to Turkish usage; README_EN.md contains the English guide.
ZIP SHA-256: 42d921fe88fb081c00e0c111f2f6f99daad701d687669b179310bc8e2a00e229.