I make an API call which returns a json response stocks like so (
entire response I am trying to loop):
{
"data": [{
"symbol": "000",
"name": "Greenvolt - Energias Renováveis, S.A.",
"currency": "EUR",
"exchange": "FSX",
"country": "Germany",
"type": "Common Stock"
}, {
"symbol": "000",
"name": "Greenvolt - Energias Renováveis, S.A.",
"currency": "EUR",
"exchange": "Munich",
"country": "Germany",
"type": "Common Stock"
}, {
Now I want to iterate over the items within data but I'm confused about the returned structure (looks like a dict in a list in a dict somewhat?)
What I've tried:
stocks = td.get_stocks_list().as_json()[0]
print(type(stocks)) # prints 'dict'
print(stocks) # however this only prints the second item, not the first nor the entire dict I want to loop:
{'symbol': '000', 'name': 'Greenvolt - Energias Renováveis, S.A.', 'currency': 'EUR', 'exchange': 'Munich', 'country': 'Germany', 'type': 'Common Stock'}
So how to catch all items within data in the response?
CodePudding user response:
Your print command is outside of the loop so it should be
stocks_new = {}
for stock in stocks:
stocks_new = stock
print(stocks_new)
CodePudding user response:
Below is the solution for the API call, using the requests module in python, in your last case if you write the print inside the loop then it will work,
import requests
response = requests.get("https://api.twelvedata.com/stocks?source=docs")
for _res in response.json()["data"]:
print(_res)
print(_res["country"])
I have also show an example on how can you access the keys like country inside the array of dict.
CodePudding user response:
Yeah since the whole object is dict where in your snapshot has the key data which is list of nasty dicts you would need to do something like this:
dict = {
“data”: [{
“symbol”: “000”,
“name”: “Greenvolt - Energias Renováveis, S.A.“,
“currency”: “EUR”,
“exchange”: “FSX”,
“country”: “Germany”,
“type”: “Common Stock”
}, {
“symbol”: “000",
“name”: “Greenvolt - Energias Renováveis, S.A.“,
“currency”: “EUR”,
“exchange”: “Munich”,
“country”: “Germany”,
“type”: “Common Stock”
}]
}
print(dict.keys())
print(list(dict.keys()))
for key in list(dict.keys()):
print(key, ‘->‘, list(dict[key]) )
for nasty_dict in list(dict[key]):
print(nasty_dict, ‘->‘, list(dict[key]).index(nasty_dict))
for k in nasty_dict:
print(k, ‘->’, nasty_dict[k])
which would output you this:
dict_keys(['data'])
['data']
data -> [{'symbol': '000', 'name': 'Greenvolt - Energias Renováveis, S.A.', 'currency': 'EUR', 'exchange': 'FSX', 'country': 'Germany', 'type': 'Common Stock'}, {'symbol': '000', 'name': 'Greenvolt - Energias Renováveis, S.A.', 'currency': 'EUR', 'exchange': 'Munich', 'country': 'Germany', 'type': 'Common Stock'}]
{'symbol': '000', 'name': 'Greenvolt - Energias Renováveis, S.A.', 'currency': 'EUR', 'exchange': 'FSX', 'country': 'Germany', 'type': 'Common Stock'} -> 0
symbol -> 000
name -> Greenvolt - Energias Renováveis, S.A.
currency -> EUR
exchange -> FSX
country -> Germany
type -> Common Stock
{'symbol': '000', 'name': 'Greenvolt - Energias Renováveis, S.A.', 'currency': 'EUR', 'exchange': 'Munich', 'country': 'Germany', 'type': 'Common Stock'} -> 1
symbol -> 000
name -> Greenvolt - Energias Renováveis, S.A.
currency -> EUR
exchange -> Munich
country -> Germany
type -> Common Stock
and that how you could iterate over all objects
CodePudding user response:
As you had written your code before the edit of your question, the print statement was placed outside of the loop and only the last element of stocks would be printed.
This should print all elements of stocks:
for stock in stocks:
print(stock)
