Python JSON

Read and write JSON data.

✍️ 3 exercises + code challenge in this lesson

The json module converts between JSON text and Python objects.

  • json.loads(text): JSON string → Python (dict, list…).
  • json.dumps(obj): Python → JSON string.
  • indent=2 pretty-prints; sort_keys=True sorts keys.
  • json.load(file) and json.dump(obj, file) work with files.

Mapping: dict ↔ object, list ↔ array, str ↔ string, True/False ↔ true/false, None ↔ null.

Key Takeaways: Python JSON

Mastering Python JSON helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python JSON Code Example

Python
import json

text = '{"name": "Sara", "courses": ["Python", "SQL"], "active": true, "manager": null}'
data = json.loads(text)
print(data["name"], data["courses"], data["active"], data["manager"])

data["courses"].append("Django")
print(json.dumps(data, indent=2))
Free sign-in required · Google login available

Python JSON Practice Exercises

Check what you learned in “Python JSON”.

/ 3 solved
Fill in the blank

Convert a Python dict to a JSON string.

text = json.(data)
✓ Correct! Not quite. Try again.
Multiple choice

What does Python None become in JSON?

✓ Correct! Not quite. Try again.
Code challenge

Parse the JSON text and print the value of "lang".

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services