Python Forum
Get JSON Value based on Another value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get JSON Value based on Another value
#1
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, }, ] }
Reply
#2
for trade in data['trades']:
    print(f"{trade['stockSymbol']}: {trade['bid']}")
where data is JSON object, i.e. after you load the JSON string
sambanerjee likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thumbs Up 
(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 -
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Split json into separate json based on node value CzarR 1 5,582 Jul-08-2022, 07:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020