.load()

StevenSwiniarski's avatar
Published May 4, 2022Updated Jun 15, 2022
Contribute to Docs

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...

All contributors

Contribute to Docs

Learn Python on Codecademy