Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a variable with Requests and JSON

I've been programming an application that pulls information from an online API, and I need some help with it.

I'm using requests, and my current code is as follows

myData = requests.get('theapiwebsitehere.com/thispartisworking')
myRealData = myData.json()
x = myRealData['data']['playerStatSummaries']['playerStatSummarySet']['maxRating']
print x

I then get this error

myRealData = myData.json()                                                                                                                      
TypeError: 'NoneType' object is not callable

I want to be able to get to the variable maxRating, and print it out, but I can't seem to do that.

Thanks for your help.

like image 718
user1198805 Avatar asked Jan 27 '13 01:01

user1198805


People also ask

How do I print JSON data?

There is another module that developers can use for pretty print JSON data. Using simplejson module, you can use its simplejson. dumps() method and pass the json object and indent value as parameters. It works similar to that of json module but is not a built-in module of Python.

How do you print your response in JSON format?

print(response. json()) should give the the data formatted as JSON for this response.

How can I get specific data from JSON?

Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.


1 Answers

Firstly is myData actually returning anything?

If it is then you can try the following rather than work with the .json() function

Import the Json package and use the Json loads function on the text.

import json
newdata = json.loads(myData.text())
like image 199
Matt Alcock Avatar answered Sep 23 '22 22:09

Matt Alcock