JSON
The json module converts Python values to JSON text and back. dumps makes a string, loads reads one, and dump and load work with files. Use indent for readable output. JSON has no tuples or sets, and dictionary keys become strings.
Example
import json
data = {"name": "Ada", "skills": ["python", "sql"], "active": True}
text = json.dumps(data)
print(text)
print(json.dumps(data, indent=2))
back = json.loads(text)
print(back["skills"][0], type(back))Result
{"name": "Ada", "skills": ["python", "sql"], "active": true}
{
"name": "Ada",
"skills": [
"python",
"sql"
],
"active": true
}
python <class 'dict'>Exercise
Turn JSON text into a Python value.
data = json.(text)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.