Codzy

The JSON format

JSON is a text format widely used to exchange data, for example between a website and a server. It looks a lot like Python dictionaries and lists.

The json module does the conversion: json.dumps() turns a Python object into JSON text, and json.loads() turns JSON text into a Python object. Careful: JSON uses true, false and null, which become True, False and None in Python.

import json

player = {"name": "Sara", "level": 3, "skills": ["python", "sql"]}
text = json.dumps(player)
print(text)

data = json.loads('{"score": 42, "ok": true}')
print(data["score"])
print(data["ok"])

Start the lesson