Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Sorry guys, I couldn't find the satisfying answer to print part of json response. Can someone help me here please:
import json
import requests
import pprint
response = requests.get('<api endpoing>')
json_data = response.json()
print(json.dumps(json_data, indent=4, sort_keys=True))
Json response would be
"Value1": "SomeValue",
"data": {
"subval1": false,
"subval2": "0a4",
"subval3": "",
"subval4": "Click h!",
"subval5": "1002",
"subval6": "932",
"subval7": "i2",
"subval8": 250,
"subval9": 0,
"subval10": 1,
"subval11": 3,
"subval12": 1,
"subval13": "<!>",
"subval14": "",
"subval15": "Click !!",
"subval16": "",
"subval17": 300
"error": true,
"message": "Success",
"status": 200
Now, I would like to traverse and print only the "data": values. I will do the following
data = json.loads(json_data)
data_set = (data['data'])
print(data_set)
But the error Im getting: TypeError: the JSON object must be str, not 'dict'
You don't need to json.loads(json_data)
as it is already a python dict, you just need to output this dict directly. And outputing json string from a dict is json.dumps()
's job :
json.dumps(json_data["data"])
Actually json()
method returns a json object,so you don't need to use json.loads
Or you can use
r=requests.get("")
print json.loads(r.content)
See more details from JSON Response Content.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.