Compare parsed JSON structures instead of raw whitespace
To compare JSON structures instead of whitespace, call json.loads() on both texts and compare the resulting Python values. Raw strings preserve formatting, so compact {"a":1} and its pretty-printed equivalent are unequal even though they describe the same JSON object. The standard conversion maps a JSON object to a Python dict; comparing those decoded dictionaries tests this example’s data structure rather than its presentation. Python JSON documentation
The program first asserts and prints the expected raw-text mismatch. It then decodes each source, prints the left Python value, and confirms that both parsed values equal {'a': 1}. This provides a small reproducible boundary: whitespace outside JSON strings changes the source text but not the decoded structure.
Parsing before comparison is not a byte-for-byte integrity check. It intentionally ignores allowed formatting differences, and it also cannot preserve every lexical choice: for example, object member order should not be treated as a formatting-independent semantic guarantee for all consumers, and the default decoder accepts repeated names by retaining the last value. If an interface requires canonical bytes, signatures, comments, or exact numeric spellings, define and compare that representation instead. No newer API is involved; the example runs on Python 3.
AI assistance disclosure: the fixture and explanation were created with AI assistance.
Example
import json
compact = '{"a":1}'
pretty = '{\n "a": 1\n}'
left = json.loads(compact)
right = json.loads(pretty)
assert compact != pretty
assert left == right == {"a": 1}
print(f"raw texts equal: {compact == pretty}")
print(left)
print(f"parsed values equal: {left == right}")
Expected output:
raw texts equal: False
{'a': 1}
parsed values equal: True