JSON Encoding and Decoding
Use json.loads, json.dumps, and json.load/dump for JSON I/O.
Introduction
Python's json module converts between Python objects and JSON strings or files with simple, consistent functions.
json.dumps()
json.dumps(obj) serializes a Python object to a JSON string. indent=2 pretty-prints it.
import json
d = {'name': 'Alice', 'age': 30, 'active': True}
print(json.dumps(d, indent=2))All lessons in this course
- JSON Encoding and Decoding
- Handling Nested JSON Structures
- Reading CSV with csv.reader
- Writing CSV with csv.DictWriter