Note: Modified original question to demonstrate exactly what I am trying to do
I have two lists of JSON objects that I need to check are the "same" e.g.
x = [
{
"Id": "A",
"Type": "A",
"Component": "A",
"Level": "A",
"Debug": "A",
"Space": 1
},
{
"Id": "B",
"Type": "B",
"Component": "B",
"Level": "B",
"Debug": "B",
}
]
y = [
{
"Id": "B",
"Type": "B",
"Component": "B",
"Level": "B",
"Debug": "B",
},
{
"Id": "A",
"Type": "A",
"Component": "A",
"Level": "A",
"Debug": "A",
"Space": 1
}
]
For the purpose of what I am doing these objects would be classed as identical.
Background: I have data returned from an API that I have no control over. I need to check if the returned data is identical to a pre-stored definition that I have. If it is identical, that is both lists contain the same number of objects and those contain the same keys, then I need to return True
. Otherwise False
. Note that the keys in the dictionaries can be in a different order.
What I have tried so far:
Performing a
json.dumps()
withsort_keys
set. This only seems to sort the top level objects and not drill down into the child objects.I have also tried in object
y
moving object A above object B. Then doing ax == y
comparison does returnTrue
.
Based on the above, it looks like the in built in equality operator expects the objects to be sorted in the same order. Therefore, I think something that can sort based on Type
and then Id
might be the way forward. The Type
field determines if the object will contain a Space
attribute, the addition of which in some of the objects seems to indicate that ordering is important.
Note 1: Performance is not important. Note 2: I am not familiar with Python so unsure which of tool to use from the toolbox, or if there is anything built in.
Thanks.