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)
| 39 | |
| 40 | |
| 41 | def 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 | |
| 55 | def canonical_json(value: Any) -> str: |
no test coverage detected