Tutorials Logic, IN info@tutorialslogic.com

Python JSON: Parse, Write, Validate, and Save Data

JSON Basics

JSON is a text format used to exchange structured data between programs, APIs, files, and web apps.

Python uses the json module to convert JSON text into dictionaries and lists, then convert Python data back into JSON text.

Treat JSON as an exchange format: parse it at the boundary, work with Python objects inside the program, then serialize when saving or sending.

Read JSON

json.loads() reads JSON text and returns Python values.

  • JSON objects become dictionaries.
  • JSON arrays become lists.
  • JSON strings must use double quotes.

Parse JSON Text

Parse JSON Text
import json

text = '{"name": "Maya", "skills": ["Python", "SQL"]}'
user = json.loads(text)

print(user["name"])
print(user["skills"][0])
Output
Maya
Python

The JSON object becomes a Python dictionary with a list inside it.

Write JSON

json.dumps() turns Python values into JSON text.

  • Use indent for readable output.
  • Use sort_keys when stable key order helps comparison.

Create JSON Text

Create JSON Text
import json

settings = {"theme": "dark", "font_size": 16}
text = json.dumps(settings, indent=2)

print(text)
Output
{
  "theme": "dark",
  "font_size": 16
}

indent=2 makes the JSON easier for humans to read.

JSON Files

json.load() reads from a file object. json.dump() writes to a file object.

  • Use with open() so the file closes safely.
  • Choose UTF-8 encoding for portable text.

JSON Errors

Invalid JSON raises json.JSONDecodeError.

  • Validate incoming JSON before trusting fields.
  • Check required keys after parsing.
  • Handle bad JSON near the input boundary.
Skill check

Can You Exchange JSON?

5 checks
  • Use json.loads() for JSON strings.
  • Use json.dumps() for Python data to JSON text.
  • Use json.load() and json.dump() for files.
  • Catch JSONDecodeError for invalid incoming JSON.
  • Validate required keys before using parsed data.

JSON Decisions

0 of 2 checked

Q1. What does json.loads(text) return for a JSON object?

Q2. What is a common JSON mistake?

JSON Data Traps

  • Confusing JSON text with Python objects

    Use json.loads to turn JSON text into Python data and json.dumps to turn Python data into JSON text.
  • Using single quotes in JSON text

    Valid JSON uses double quotes for object keys and string values.
  • Trusting every field exists

    Validate required keys before using JSON from files or APIs.

Try this next

Save and Load Structured Data

0 of 3 completed

  1. Load a JSON string with name and email, then print both fields.
  2. Dump a dictionary with indentation so it is readable.
  3. Print a clear message when an expected email key is missing.

Questions About JSON

No. JSON is text. A Python dictionary is an in-memory object. json.loads() converts JSON text into Python data.

The JSON format requires double quotes around object keys and string values. Single quotes are not valid JSON.

No. JSON stores simple data such as objects, arrays, strings, numbers, booleans, and null.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.