How to use the JSON module in Python

1. Import the JSON module:

import json

2. Load JSON data:

with open(‘data.json’) as json_file:
data = json.load(json_file)

3. Access the data:

for item in data:
print(item[‘name’], item[‘age’])

4. Create JSON data:

data = {
‘name’: ‘John Smith’,
‘age’: 42
}

with open(‘data.json’, ‘w’) as json_file:
json.dump(data, json_file)