Reputation: 11
I can't figure out how to print a specific key/value from a string of JSON data after using requests.get. My understanding is that, when using request.get, the data is formatted as a dictionary by Python. As such, I thought I would be able to print any key/value I wanted by treating it like a normal dictionary: x = thisdict["brand"]. So far I've only managed to get it to print the full JSON string, which isn't what I need.
import requests
# Pulling ISS position data as json string.
r = requests.get('http://api.open-notify.org/iss-now.json?print=pretty')
r.json()
# Printing
print (r.text)
How would I print the current key/value ('message': 'status') from the json string?
Upvotes: 1
Views: 183
Reputation: 20434
One of your lines currently reads r.json()
. This parses the result of the request as a JSON string and returns the result which you are not assigning to any variable.
Instead try something like:
import requests
r = requests.get('http://api.open-notify.org/iss-now.json?print=pretty')
r_json = r.json()
print('message:', r_json['message'])
which gives:
message: success
Upvotes: 2