AttributeError("'str' object has no attribute 'read'")
The problem is that for json.load
you should pass a file like object with a read
function defined. So either you use json.load(response)
or json.loads(response.read())
.
Ok, this is an old thread but. I had a same issue, my problem was I used json.load
instead of json.loads
This way, json has no problem with loading any kind of dictionary.
json.load - Deserialize fp (a .read()-supporting text file or binary file containing a JSON document) to a Python object using this conversion table.
json.loads - Deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object using this conversion table.
If you get a python error like this:
AttributeError: 'str' object has no attribute 'some_method'
You probably poisoned your object accidentally by overwriting your object with a string.
How to reproduce this error in python with a few lines of code:
#!/usr/bin/env pythonimport jsondef foobar(json): msg = json.loads(json)foobar('{"batman": "yes"}')
Run it, which prints:
AttributeError: 'str' object has no attribute 'loads'
But change the name of the variablename, and it works fine:
#!/usr/bin/env pythonimport jsondef foobar(jsonstring): msg = json.loads(jsonstring)foobar('{"batman": "yes"}')
This error is caused when you tried to run a method within a string. String has a few methods, but not the one you are invoking. So stop trying to invoke a method which String does not define and start looking for where you poisoned your object.