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

I want to extract JSON objects that have a specific key value pair present in them

I have a JSON response from an API that looks like this:

"get_result_by_result_id": [{
    "data": {
        "date_trunc": "2022-09-06T00:00:00+00:00",
        "project": "Smoothy Finance",
        "usd_volume": 42307.09145419092
    },
     
}, {
    "data": {
        "date_trunc": "2022-09-30T00:00:00+00:00",
        "project": "curve",
        "usd_volume": 40548.688138892685
    },
     
},  {
    "data": {
        "date_trunc": "2022-09-14T00:00:00+00:00",
        "project": " kyber",
        "usd_volume": 13800038.727002844
    },
     
},]

And I want to extract all objects in the response which have "kyber" value present in them like this:

"common_objects": [{
        "data": {
            "date_trunc": "2022-09-14T00:00:00+00:00",
            "project": " kyber",
            "usd_volume": 13800038.727002844
        },
         
    }, {
        "data": {
            "date_trunc": "2022-09-07T00:00:00+00:00",
            "project": " kyber",
            "usd_volume": 24518356.073168807
        },},]

How should I do this?

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

>Solution :

I think your json is corrupted, so I edited it a bit and here’s how you can do it:

data = [{
    'data': {
        'date_trunc': '2022-09-06T00:00:00+00:00',
        'project': 'Smoothy Finance',
        'usd_volume': 42307.09145419092
    }

}, {
    'data': {
        'date_trunc': '2022-09-30T00:00:00+00:00',
        'project': 'curve',
        'usd_volume': 40548.688138892685
    }

},  {
    'data': {
        'date_trunc': '2022-09-14T00:00:00+00:00',
        'project': ' kyber',
        'usd_volume': 13800038.727002844
    }

}]

clear_data = []
for data in data:
    if(data['data']['project'] == ' kyber'):
        clear_data.append(data)

print(clear_data)
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