Skip to content

Instantly share code, notes, and snippets.

@Nyahua
Last active March 26, 2017 12:09
Show Gist options
  • Save Nyahua/726ca72fd0471067be889a405a8e642f to your computer and use it in GitHub Desktop.
Save Nyahua/726ca72fd0471067be889a405a8e642f to your computer and use it in GitHub Desktop.
What is the difference between json.dumps and json.load? [closed]

What is the difference between json.dumps and json.load? [closed]

dumps takes an object and produces a string:

>>> a = {'foo': 3}
>>> json.dumps(a)
'{"foo": 3}'

load would take a file-like object, read the data from that object, and use that string to create an object:

with open('file.son') as fh:
    a = json.load(fh)

Note that dump and load convert between files and objects, while dumps and loads convert between strings and objects. ** You can think of the s functions as wrappers around the s-less functions: **

def dump(obj, fh):
    fh.write(dumps(obj))

def load(fh):
    return loads(fh.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment