Python Parse JSON Response from URL - python

I'm am wanting to get information about my Hue lights using a python program. I am ok with sorting the information once I get it, but I am struggling to load in the JSON info. It is sent as a JSON response. My code is as follows:
import requests
import json
response= requests.get('http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights')
data = json.load(response)
print(data)
When this is run, all I get is the error:
in load return loads(fp.read(),
Response' object has no attribute 'read'

The problem is you are passing in the actual response which consists of more than just the content. You need to pull the content out of the response:
import requests
r = requests.get('https://github.com/timeline.json')
print r.text
# The Requests library also comes with a built-in JSON decoder,
# just in case you have to deal with JSON data
import requests
r = requests.get('https://github.com/timeline.json')
print r.json
http://www.pythonforbeginners.com/requests/using-requests-in-python
Looks like it will parse the JSON for you already...

Use response.content to access response content and json.loads method instead of json.load:
data = json.loads(response.content)
print data

Related

Show Json Data from Python

How do i make Json requests to an endpoint to show the response in Json rather than show the response
i sent a POST request to a server and i am getting this as response.
D:\python>python runVacc.py
<Response [200]>
Which means its a successful request, but i want it to show both the Success as well as the Json Response. My Code is looking thus :
import requests
import json
url = 'URL HERE'
data = {"email":"tim#john.com","is_permanent":True,"bvn":"12345678901","tx_ref":"VAL12","phonenumber":"08098688235","firstname":"John","lastname":"Fish","narration":"Test ACC"}
res = requests.post(url, data=json.dumps(data),headers={'Content-Type':'application/json','Authorization':'Bearer FLWSECK_TEST-2033696a107e162088cdb02f777fa44e-X'})
print(res)
How do I make it to print Json Response containing the Json Data?
Please help, I am new to this.
Use print(res.content) instead.

Get the specific response parameter with urllib in python

I am able to perform a web request and get back the response, using urllib.
from urllib import request
from urllib.parse import urlencode
response = request.urlopen(req, data=login_data)
content = response.read()
I get back something like b'{"token":"abcabcabc","error":null}'
How will i be able to parse the token information?
You can use the json module to load the binary string data and then access the token property:
token = json.loads(bin_data)['token']

PUT method using python3 and urbllib - headers

So I am trying to just receive the data from this json. I to use POST, GET on any link but the link I am currently trying to read. It needs [PUT]. So I wanted to know if I was calling this url correctly via urllib or am I missing something?
Request
{"DataType":"Word","Params":["1234"], "ID":"22"}
Response {
JSON DATA IN HERE
}
I feel like I am doing the PUT method call wrong since it is wrapped around Request{}.
import urllib.request, json
from pprint import pprint
header = {"DataType":"Word","Params":"[1234"]", "ID":"22"}
req = urllib.request.Request(url = "website/api/json.service", headers =
heaer, method = 'PUT')
with urllib.request.urlopen(req) as url:
data = json.loads(url.read(), decode())
pprint(data)
I am able to print json data as long as its anything but PUT. As soon as I get a site with put on it with the following JSON template I get an Internal Error 500. So I assumed it was was my header.
Thank you in advanced!

Python 3.4.1 - Reading HTTP Request Data

I'm fairly new to Python and I'm trying to execute a HTTP Request to a URL that returns JSON. The code, I have is:
url = "http://myurl.com/"
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
data = response.read()
I'm getting an error reading: "'bytes' object has no attribute 'read'". I searched around, but haven't found a solution. Any suggestions?
You may find the requests library easier to use:
import requests
data = requests.get('http://example.com').text
or, if you need the raw, undecoded bytes,
import requests
data = requests.get('http://example.com').content

Simple Json issue datatype

I'm trying to workout this simple json issue in Python but just can't work out where im going wrong. I have tried using json.dumps() but just either get unicode object has no attribute get or str has not attribute get.
heres my code
import urllib2
import json
url = "http://xxxxxxxxxxxxxxxxxxxxxx"
request = urllib2.Request(url)
response = urllib2.urlopen(request)
data = response.read()
js = json.loads(data)
json = json.dumps(js)
for item in json:
#print item['top']['buyorders'][0]["price"]
print item.get('top').get('buyorders')[0].get['price']
Don't do the dump, pass the response directly to json.load() and iterate over the produced python dictionary:
data = json.load(response)
for key, value in data.iteritems():
...

Categories