0
with open(r'E:\file\file.json', 'r') as data_compare:
    d1 = json.load(data_compare)
    set1 = set(d1)

with open(r'E:\file\file2.json', 'r') as data_updated:
    d2 = json.load(data_updated)
    set2 = set(d2)

    compare = set2 - set1
    print(compare)

the output of this is only 'set()' for some reason, can anyone explain what i am doing wrong? Here is a small sample of one of my json files (the other is identical in terms of structure and keys, the values only differ)

{
    "data": [
        {
            "Process": "proc",
            "num": "00000",
        },
        {
            "Process": "process",
            "num": "00101",

       }
   ]
}

THIS IS NOT A DUPLICATE, this involves loading data from a json to a python object, not comparing pre existing python dictionaries

EcSync
  • 842
  • 1
  • 6
  • 20
  • @BearBrown i have made changes to my question to explain why this is not a duplicate and why you didnt read my question properly – EcSync Mar 05 '19 at 12:41
  • i think it is no matter – Brown Bear Mar 05 '19 at 12:43
  • 2
    How are dictionaries you initialized with `json.load` different from other dictionaries? (Hint: They're not.) – tripleee Mar 05 '19 at 12:43
  • 1
    You are converting dictionnaries to sets. This gives a set containing only the keys of the dictionnaries and discarding the values. So the two sets are only `set(['data'])`. This is not exactly a duplicate but was about *what gives a dict when converted to a set*. BTW, printing the content of the dicts and the sets would have been enough to find the solution... – Serge Ballesta Mar 05 '19 at 12:54
  • @SergeBallesta thanks for the repsonse! I am still having difficulty comparing the two JSONs as it returns a 0 for amount of different keys – EcSync Mar 05 '19 at 12:59
  • @EcSync: My advice is to *print* what you compare, and if it is not enough print intermediary data. Python can be used in interactive mode, and it is a nice way to understand what happens. – Serge Ballesta Mar 05 '19 at 13:01
  • `set(d2)` returns a set of keys only. Add this to your code to get different of dictionaries: `diff = {i:d2[i] for i in d2 if i in compare} ` and `print(diff)`. – Heyran.rs Mar 05 '19 at 13:06

0 Answers0