Home > Mobile >  get a value of key from json response python
get a value of key from json response python

Time:02-11

I am accessing an API through a get request from which I get a response.

this is my code :

import requests
import json
symbol = "AAPL"
end_point="https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers/" symbol "?apiKey=my_key"

r=requests.get(end_point).json()


print(r)
print(r['min'])

print(r) returned:

{'request_id': '2735c0be51de7719fd99460fe8696080', 'status': 'OK', 'ticker': {'day': {'c': 172.93, 'h': 175.48, 'l': 172.37, 'o': 174.14, 'v': 65575561, 'vw': 174.0984}, 'lastQuote': {'P': 172.83, 'S': 3, 'p': 172.82, 's': 1, 't': 1644524332922450142}, 'lastTrade': {'c': None, 'i': '139592', 'p': 172.8199, 's': 2014, 't': 1644524331573573011, 'x': 4}, 'min': {'av': 65559987, 'c': 172.9, 'h': 173.14, 'l': 172.89, 'o': 173.09, 'v': 107429, 'vw': 173.0138}, 'prevDay': {'c': 176.28, 'h': 176.65, 'l': 174.9, 'o': 176.05, 'v': 71204538, 'vw': 175.8287}, 'ticker': 'AAPL', 'todaysChange': -3.46, 'todaysChangePerc': -1.963, 'updated': 1644524331573573011}}

but when I try to access the key "min" , I get key error :

KeyError: 'min'

this is super simple and i cant figure out what in world am i doing wrong. please advice :)

CodePudding user response:

Please try:

r['ticker']['min']

CodePudding user response:

the key min is inside ticker. Do this:

r['ticker']['min']
  • Related