The json.load() method (without "s" in "load") can read a file directly:
import json
with open('strings.json') as f:
d = json.load(f)
print(d)
You were using the json.loads() method, which is used for string arguments only.
The error you get with json.loads is a totally different problem. In that case, there is some invalid JSON content in that file. For that, I would recommend running the file through a JSON validator.
There are also solutions for fixing JSON like for example How do I automatically fix an invalid JSON string?.
Answer from ubomb on Stack OverflowThe json.load() method (without "s" in "load") can read a file directly:
import json
with open('strings.json') as f:
d = json.load(f)
print(d)
You were using the json.loads() method, which is used for string arguments only.
The error you get with json.loads is a totally different problem. In that case, there is some invalid JSON content in that file. For that, I would recommend running the file through a JSON validator.
There are also solutions for fixing JSON like for example How do I automatically fix an invalid JSON string?.
Here is a copy of code which works fine for me,
import json
with open("test.json") as json_file:
json_data = json.load(json_file)
print(json_data)
with the data
{
"a": [1,3,"asdf",true],
"b": {
"Hello": "world"
}
}
You may want to wrap your json.load line with a try catch, because invalid JSON will cause a stacktrace error message.
JSON load() vs loads()
load() loads JSON from a file or file-like object
loads() loads JSON from a given string or unicode object
It's in the documentation
More on reddit.comProcessing large JSON files in Python without running out memory
Want to merge multiple JSON files into one
A memoryerror likely indicates that your json files are too large. I'd look into reducing the size of the files you retrieved if possible.
I found this stackoverflow that also suggests a python library called ijson. But I've never used it... https://stackoverflow.com/questions/40399933/memoryerror-when-loading-a-json-file
More on reddit.comHelp with double quotes in json
Videos
Can someone explain what the difference is between using either load() or loads() is with the JSON library? And which, if either, is the preferred method.
I'm writing a simple script where I want the JSON data from a URL parsed out into a list. Both of these options seem to work:
import json import urllib2 url = "string to url" response = urllib2.urlopen(url) data = json.load(response)
or
import json import urllib2 url = "string to url" response = urllib2.urlopen(url) data = json.loads(response.read())
I know that there are other libraries available for parsing out JSON data, but for the time being I'm working only with the json and urllib2 libraries.
Any insight into which one should be used?
Thanks
load() loads JSON from a file or file-like object
loads() loads JSON from a given string or unicode object
It's in the documentation
The "s" is an abbreviation for "string". "dump__s__" is read as "dump string". "load__s__" = "load string". Otherwise these methods want a file-like object. This convention is scattered throughout python and even 3rd-party packages.