0

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() with sort_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 a x == y comparison does return True.

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.

Remotec
  • 10,304
  • 25
  • 105
  • 147
  • 5
    JSON is a textual serialisation format; if you want to compare their contents, just deserialize into Python objects and test if they are equal. Python dictionaries are also order agnostic. – Martijn Pieters Jan 23 '19 at 15:35
  • What you have is not valid JSON. You are missing commas. – pault Jan 23 '19 at 15:39
  • 1
    Possible duplicate of [How to compare two JSON objects with the same elements in a different order equal?](https://stackoverflow.com/questions/25851183/how-to-compare-two-json-objects-with-the-same-elements-in-a-different-order-equa) – pault Jan 23 '19 at 16:02

1 Answers1

0

Json are just serialized objects, you can loads into python dicts with the json module and the loads or load method (depends on your input) then just use the == operator

import json
d1 = json.loads("""[{
    "a": 1,
    "b": 2,
    "c": {
        "x": 9,
        "y": 10
    }
},
{
    "a": 10,
    "b": 20,
    "c": {
        "x": 90,
        "y": 100
    }
}]""")

d2 = json.loads("""[{
    "b": 2,
    "a": 1,
    "c": {
        "x": 9,
        "y": 10
    }
},
{
    "a": 10,
    "b": 20,
    "c": {
        "y": 100,
        "x": 90
    }
}]""")
print(d1 == d2) # True

As a side notes, your json are not valids, I had to add some commas

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42