.load()

The .load() function decodes a JSON file and returns a Python object. The decoding conversion is based on the following table. The .loads() function, alternatively, takes a JSON string and returns a Python object.

Syntax

json.load(json_file)

json.loads(json_string)

Example

The json library can be used to encode and decode a dictionary:

import json
foo = {'first':'hello', 'second':'world'}
bar = json.dumps(foo)
print(type(bar))
eggs = json.loads(bar)
print(type(eggs))

The output will look like this:

<class 'str'>
<class 'dict'>

Codebyte Example

The .load() function can be used to convert a JSON file to a Python object:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy