Home > Software design >  Extract value from list inside JSON dictionary (Python)
Extract value from list inside JSON dictionary (Python)

Time:11-05

I need to extract a value from a dictionary, inside a list, inside a dictionary.
The value I'm tryng to get is dealId inside affectedDeals. (highlighted with ^)

data = {'date': '2022-11-04T12:36:57.016', 'status': 'OPEN', 'reason': 'SUCCESS', 'dealStatus': 'ACCEPTED', 'epic': 'SILVER', 'dealReference': 'o_0bc30104-8ddf-4d67-9daa-e7d878a8cad9', 'dealId': '006011e7-0055-311e-0000-000080507631', 'affectedDeals': [{'dealId': '006011e7-0055-311e-0000-000080507633', 'status': 'OPENED'}], 'level': 20.138, 'size': 1.0, 'direction': 'BUY', 'guaranteedStop': False, 'trailingStop': False}                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                         ^
output = 006011e7-0055-311e-0000-000080507633

CodePudding user response:

May be this could help you to get the output as expected in question

Code:

data = {
    "date": "2022-11-04T12:36:57.016",
    "status": "OPEN",
    "reason": "SUCCESS",
    "dealStatus": "ACCEPTED",
    "epic": "SILVER",
    "dealReference": "o_0bc30104-8ddf-4d67-9daa-e7d878a8cad9",
    "dealId": "006011e7-0055-311e-0000-000080507631",
    "affectedDeals": [
        {"dealId": "006011e7-0055-311e-0000-000080507633", "status": "OPENED"}
    ],
    "level": 20.138,
    "size": 1.0,
    "direction": "BUY",
    "guaranteedStop": False,
    "trailingStop": False,
}

[i.get("dealId") for i in data.get("affectedDeals")]

Output :

006011e7-0055-311e-0000-000080507633

Output will be enclosed in list you can use indexing to retrieve the data from list like below

lst = [i.get("dealId") for i in data.get("affectedDeals")]
lst[0] 

CodePudding user response:

Try doing this:

data = {'date': '2022-11-04T12:36:57.016', 
        'status': 'OPEN',
        'reason': 'SUCCESS',
        'dealStatus': 'ACCEPTED',
        'epic': 'SILVER',
        'dealReference': 'o_0bc30104-8ddf-4d67-9daa-e7d878a8cad9',
        'dealId': '006011e7-0055-311e-0000-000080507631',
        'affectedDeals': [
            {'dealId': '006011e7-0055-311e-0000-000080507633',
             'status': 'OPENED'
            }
        ],
        'level': 20.138,
        'size': 1.0,
        'direction': 'BUY',
        'guaranteedStop': False,
        'trailingStop': False
        }                                                                                                                                                                                            

print(data['affectedDeals'][0]['dealId'])

# Output: 006011e7-0055-311e-0000-000080507633

We basically accessed the list(affectedDeals) and then selected the 0th index, which is again a dictionary, so we accessed the specified key(dealID) and then print it.

Hope that helps
  • Related