Yes, s stands for string. The json.loads function does not take the file path, but the file contents as a string. Look at the documentation.
Simple example:
with open("file.json") as f:
data = json.load(f) # ok
data = json.loads(f) # not ok, f is not a string but a file
text = '{"a": 1, "b": 2}' # a string with json encoded data
data = json.loads(text)
Answer from Gijs on Stack OverflowYes, s stands for string. The json.loads function does not take the file path, but the file contents as a string. Look at the documentation.
Simple example:
with open("file.json") as f:
data = json.load(f) # ok
data = json.loads(f) # not ok, f is not a string but a file
text = '{"a": 1, "b": 2}' # a string with json encoded data
data = json.loads(text)
Just going to add a simple example to what everyone has explained,
json.load()
json.load can deserialize a file itself i.e. it accepts a file object, for example,
# open a json file for reading and print content using json.load
with open("/xyz/json_data.json", "r") as content:
print(json.load(content))
will output,
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
If I use json.loads to open a file instead,
# you cannot use json.loads on file object
with open("json_data.json", "r") as content:
print(json.loads(content))
I would get this error:
TypeError: expected string or buffer
json.loads()
json.loads() deserialize string.
So in order to use json.loads I will have to pass the content of the file using read() function, for example,
using content.read() with json.loads() return content of the file,
with open("json_data.json", "r") as content:
print(json.loads(content.read()))
Output,
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
That's because type of content.read() is string, i.e. <type 'str'>
If I use json.load() with content.read(), I will get error,
with open("json_data.json", "r") as content:
print(json.load(content.read()))
Gives,
AttributeError: 'str' object has no attribute 'read'
So, now you know json.load deserialze file and json.loads deserialize a string.
Another example,
sys.stdin return file object, so if i do print(json.load(sys.stdin)), I will get actual json data,
cat json_data.json | ./test.py
{u'event': {u'id': u'5206c7e2-da67-42da-9341-6ea403c632c7', u'name': u'Sufiyan Ghori'}}
If I want to use json.loads(), I would do print(json.loads(sys.stdin.read())) instead.
Stdlib json.load[s]() return type - Typing - Discussions on Python.org
Why json.dump() and .load() are really needed?
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.comEfficiently Load Large JSON Files Object by Object
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.
Hi, hope everyone is well.
Just nearing the basics end of PCC book, I'm at saving user's data now. What exactly is the reason, when storing simple data, to use json.dump() or load(), instead of just saving and then reading it from simple text file?
I just can't place it in my head why do I really need it and it always makes it more difficult for me to learn if that's the case.
Thank you all in advance.