Read and Write JSON in Python
Python's standard json module parses JSON into native values and serializes native values back to JSON text.
What is Read and Write JSON?
Python's standard json module parses JSON into native values and serializes native values back to JSON text.
Convert between JSON text and Python dictionaries.
When should you use it?
- Learn Python syntax through a practical example.
- Build a foundation for larger programming problems.
- Test an idea quickly without a local setup.
Example code
main.py
import json
user = json.loads('{"name": "Ada", "active": true}')
user["score"] = 95
print(json.dumps(user, indent=2))
Expected output
{
"name": "Ada",
"active": true,
"score": 95
}
How it works
Loads parses a JSON string. Dumps serializes the updated dictionary with indentation for readable output.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.