MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / normalize_json

Function normalize_json

scripts/compare_backend_snapshots.py:41–52  ·  view source on GitHub ↗

Normalize JSON recursively while ignoring ordering differences. - dict keys are sorted by name - lists are normalized and then sorted by canonical JSON representation

(value: Any)

Source from the content-addressed store, hash-verified

39
40
41def normalize_json(value: Any) -> Any:
42 """Normalize JSON recursively while ignoring ordering differences.
43
44 - dict keys are sorted by name
45 - lists are normalized and then sorted by canonical JSON representation
46 """
47 if isinstance(value, dict):
48 return {k: normalize_json(value[k]) for k in sorted(value.keys())}
49 if isinstance(value, list):
50 items = [normalize_json(v) for v in value]
51 return sorted(items, key=lambda x: json.dumps(x, sort_keys=True, ensure_ascii=False))
52 return value
53
54
55def canonical_json(value: Any) -> str:

Callers 3

compare_listsFunction · 0.85
compare_json_recursiveFunction · 0.85
extract_entitiesFunction · 0.85

Calls 1

keysMethod · 0.80

Tested by

no test coverage detected