Python Json Module: Reference and Examples

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.


Reads a valid json string into a variable

import json

json.loads("""{"foo":123, "bar": "baz"}""")
# >>> {'foo': 123, 'bar': 'baz'}


Prints dictionaries, lists and primitive types such as arrays

import json

json.dumps({"foo":123, "bar":"baz"})
# >>> '{"foo": 123, "bar": "baz"}'

json.load vs json.loads

json.load json.loads
Read a json file into a python variableRead a json string into a python variable
json.load(open("myfile.json"))json.loads("""{"foo":123, "bar": "baz"}""")

decode Python values to json values

Clas JSONDecoderperforms the following conversions by default:

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

See also

Dialogue & Discussion