1

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?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.