Home > Software engineering >  Compare two separate JSONs
Compare two separate JSONs

Time:01-06

I have a resultant json from an intermediate stage as following

a=[{
    "ID": "1201",
    "SubID": "S1201",
    "Information": {
        "Name": "Kim",
        "Age": "41"
    }
}, {
    "ID": "1433",
    "subID": "G1433",
    "Information": {
        "Name": "John",
        "Age": "32"
    }
}]

I have another json that needs to compared with the above json

c= [{
            "ID": "1201",
            "SubID": "S1201"
        },
        {
            "ID": "3211",
            "subID": "G3211"
        }
    ]

since the json object(a) in my intermediate result is present in another json(c). I want to retain only the json object which is being repeated.

expected output:

[{
    "ID": "1201",
    "SubID": "S1201",
    "Information": {
        "Name": "Kim",
        "Age": "41"
    }
}]

I'm not clear on what the approach to proceed with in achieving the same. Please guide me on this. Thanks.

CodePudding user response:

ids = [e['ID'] for e in c]
repeated = [e for e in a if e['ID'] in ids]
print(repeated)
  •  Tags:  
  • Related