Extract information from JSON output
Question:
I am trying to extract information from a JSON. I have been able to extract some parts without problem, but I have problems extracting strings at very low levels.
I got an output of what I need in this format. This I have stored in a list. Here is the example:
prices = [{'info': {'price': '2.00', 'desc': 'Up'}},
{'info': {'price': '1.50', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Up'}},
{'info': {'price': '8.40', 'desc': 'Down'}},
{'info': {'price': '14.20', 'desc': 'Down'}}]
From this list I would like to extract the elements price
and desc
from each of them, stored in a list each.
I have tried to pass this list to a str string and use operators to search directly in the content, but I don’t know how to do it in a simpler way.
Do you know a simpler way to do it?
Answers:
price = [i['info']['price'] for i in prices]
desc = [i['info']['desc'] for i in prices]
JSON is a serialization format. Once you’ve decoded it, its just python. In your example, price
is a list
of dict
with a sub-dict. One option is to iterate the list, then get the dictionary information based off of known key names.
prices = [{'info': {'price': '2.00', 'desc': 'Up'}},
{'info': {'price': '1.50', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Up'}},
{'info': {'price': '8.40', 'desc': 'Down'}},
{'info': {'price': '14.20', 'desc': 'Down'}}]
for data in prices:
price = data['info']['price']
desc = data['info']['desc']
print(price, desc)
output
2.00 Up
1.50 Down
3.00 Down
3.00 Up
8.40 Down
14.20 Down
I am trying to extract information from a JSON. I have been able to extract some parts without problem, but I have problems extracting strings at very low levels.
I got an output of what I need in this format. This I have stored in a list. Here is the example:
prices = [{'info': {'price': '2.00', 'desc': 'Up'}},
{'info': {'price': '1.50', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Up'}},
{'info': {'price': '8.40', 'desc': 'Down'}},
{'info': {'price': '14.20', 'desc': 'Down'}}]
From this list I would like to extract the elements price
and desc
from each of them, stored in a list each.
I have tried to pass this list to a str string and use operators to search directly in the content, but I don’t know how to do it in a simpler way.
Do you know a simpler way to do it?
price = [i['info']['price'] for i in prices]
desc = [i['info']['desc'] for i in prices]
JSON is a serialization format. Once you’ve decoded it, its just python. In your example, price
is a list
of dict
with a sub-dict. One option is to iterate the list, then get the dictionary information based off of known key names.
prices = [{'info': {'price': '2.00', 'desc': 'Up'}},
{'info': {'price': '1.50', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Down'}},
{'info': {'price': '3.00', 'desc': 'Up'}},
{'info': {'price': '8.40', 'desc': 'Down'}},
{'info': {'price': '14.20', 'desc': 'Down'}}]
for data in prices:
price = data['info']['price']
desc = data['info']['desc']
print(price, desc)
output
2.00 Up
1.50 Down
3.00 Down
3.00 Up
8.40 Down
14.20 Down