16

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'
bnlucas
  • 163
  • 1
  • 1
  • 4
  • 2
    Your JSON has an extra bracket. Is that intentional? – Blender May 16 '13 at 01:33
  • 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? – abarnert May 16 '13 at 01:34
  • 1
    `type(data.read())` shouldn't work if `data` is a string. – Blender May 16 '13 at 01:35
  • 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. – abarnert May 16 '13 at 01:36
  • The extra bracket was a typo. Sorry about that. The data.read() was a typo. – bnlucas May 16 '13 at 02:07
  • possible duplicate of [Convert string to JSON using Python](http://stackoverflow.com/questions/4528099/convert-string-to-json-using-python) – Chillar Anand Sep 13 '15 at 16:57

1 Answers1

33

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()
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Or, instead of turning `json.load(request.read())` into `json.loads(request.read())`, just call `json.load(request)`. – abarnert May 16 '13 at 01:38
  • 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. – bnlucas May 16 '13 at 02:13
  • ` json = requests.get(url).json()` did not work for me as well my original line of code `pageContent = requests.get(url,verify=False).json()` – Umesh Kaushik Apr 13 '17 at 10:01
  • Limitation: the data parameter a=has to be <= 1GB, otherwise it will fail – MMEL May 22 '22 at 23:02