Convert string to JSON in Python?
Asked Answered
A

1

17

I'm trying to convert a string, generated from an http request with urllib3.

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    data = json.load(data)
  File "C:\Python27\Lib\json\__init__.py", line 286, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

>>> import urllib3
>>> import json
>>> request = #urllib3.request(method, url, fields=parameters)
>>> data = request.data

Now... When trying the following, I get that error...

>>> json.load(data) # generates the error
>>> json.load(request.read()) # generates the error

Running type(data) and type(data.read()) both return <type 'str'>

data = '{"subscriber":"0"}}\n'
Amplexicaul answered 16/5, 2013 at 1:31 Comment(6)
Your JSON has an extra bracket. Is that intentional?Sprawl
What do you mean "Convert string to JSON"? JSON is a string format. You want to convert JSON to the appropriate native Python objects (in this case a dict mapping one string to another)? Or some non-JSON string into a JSON string, or something different?Joly
type(data.read()) shouldn't work if data is a string.Sprawl
In fact, type(data.read()) is guaranteed to raise the exact same exception as json.load(data). I think he meant type(request.read()), which will successfully return the str type.Joly
The extra bracket was a typo. Sorry about that. The data.read() was a typo.Amplexicaul
possible duplicate of Convert string to JSON using PythonAlpinist
S
37

json.load loads from a file-like object. You either want to use json.loads:

json.loads(data)

Or just use json.load on the request, which is a file-like object:

json.load(request)

Also, if you use the requests library, you can just do:

import requests

json = requests.get(url).json()
Sprawl answered 16/5, 2013 at 1:32 Comment(4)
Or, instead of turning json.load(request.read()) into json.loads(request.read()), just call json.load(request).Joly
I am using the requests library, though it is currently commented out. Working on Google Apps Engine which wasn't allowing me to run it, and urlfetch was having issues on the same GET request. So, they support raw urllib3 and that's what I'm testing with. json.loads(request.data) is wokring, json.load(request) does not. Thanks for the help.Amplexicaul
` json = requests.get(url).json()` did not work for me as well my original line of code pageContent = requests.get(url,verify=False).json()Rosierosily
Limitation: the data parameter a=has to be <= 1GB, otherwise it will failDebris

© 2022 - 2024 — McMap. All rights reserved.