Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare two separate JSONs

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

ids = [e['ID'] for e in c]
repeated = [e for e in a if e['ID'] in ids]
print(repeated)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading