Bob Bob - 5 months ago 17
JSON Question

python: is there a well-known function to normalize JSON representation of data?

Suppose, you've got a pair of variables:

They contain JSON/JSON-array data and you need to check, if those data are identical (i.e. the dicts/lists aren't the same, so they're not equal, but they contain identical data).

There's an obstacle for python JSON-like data comparison: Dicts with data may be equal, but due to the fact that they're unordered, they may have different representations, if we compare their

"label": "JobName",
"inputBinding": {"position": 1},
"type": ["string"],
"id": "displayName",
"description": "SomeId to code sample"
"id": "displayName",
"label": "JobName",
"description": "SomeId to code sample",
"type": ["string"],
"inputBinding": {"position": 1}

Is there a well-known function in some library to normalize representation of these data?

It should be recursive and account for nested dicts and lists.


To compare these objects you need to compare loaded dicts.

assert json.loads(s1) == json.loads(s2)

To ensure serializations are consistent with each other you may use keyword argument sort_keys.

If sort_keys is True (default: False), then the output of dictionaries will be sorted by key.

assert json.dumps(d1, sort_keys=True) == json.dumps(d2, sort_keys=True)

If d1 == d2, equality above holds for any serializable data (including nested structures).