Python Forum

Full Version: Get JSON Value based on Another value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi -

This is the JSON below. I'm looking to get the value of 'bid' for each 'StockSymbol' - any help for this newbie ?

Output:
{ "currentPage":1, "trades":[ { "stockSymbol":"AAPL", "companyName":"Apple", "bid":130.17, "ask":133.42, "note":"None", "earningDueDate":"2021-01-27T00:00:00", "earningDueDateOption":0, "earningDueDateEstimated":false, "imageId":-1, "state":1, "isOptions":false, }, { "stockSymbol":"MTCH", "companyName":"Match Group", "bid":130.17, "ask":133.42, "note":"None", "earningDueDate":"2021-01-27T00:00:00", "earningDueDateOption":0, "earningDueDateEstimated":false, "imageId":-1, "state":1, "isOptions":false, }, ] }
for trade in data['trades']:
    print(f"{trade['stockSymbol']}: {trade['bid']}")
where data is JSON object, i.e. after you load the JSON string
(Jan-14-2021, 04:50 PM)buran Wrote: [ -> ]
for trade in data['trades']:
    print(f"{trade['stockSymbol']}: {trade['bid']}")
where data is JSON object, i.e. after you load the JSON string


Thank you - I'll try this out (I'm sure it will work) - I got by using the jmespath library -