Unit 9 • Lesson 4

Introduction to JSON and Data Parsing

Overview

JSON (JavaScript Object Notation) is a lightweight format for exchanging data between programs. You'll learn how to load, read, and modify JSON files in Python using the json module for real-world data projects, working with modern data formats.

Beginner 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • What JSON is: JavaScript Object Notation format.
  • JSON structure: Objects, arrays, and data types.
  • Parsing JSON: Converting JSON strings to Python objects.
  • Creating JSON: Converting Python objects to JSON strings.
  • Working with JSON files: Reading and writing JSON data.

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight data format used to exchange data between programs. It's human-readable and easy for computers to parse.

Why JSON?

  • Human-readable format
  • Language-independent
  • Widely supported
  • Commonly used in APIs
  • Easy to parse
Example JSON Data
{
  "name": "Alice",
  "age": 25,
  "city": "New York",
  "hobbies": ["reading", "coding", "hiking"],
  "active": true
}

JSON Data Types

JSON supports these data types:

Strings

"text" - always in double quotes

Numbers

42 or 3.14 - integers or floats

Booleans

true or false

Objects

{ "key": "value" } - like Python dictionaries

Arrays

[1, 2, 3] - like Python lists

Null

null - like Python None

Working with JSON in Python

Python's json module makes it easy to work with JSON. The module comes built-in with Python, so you don't need to install anything!

Parsing JSON Strings
import json

# JSON string (note: uses double quotes)
json_string = '{"name": "Alice", "age": 25, "city": "New York"}'

# Parse JSON string to Python dictionary
data = json.loads(json_string)
print(data["name"])   # Output: Alice
print(data["age"])    # Output: 25
print(type(data))     # Output: <class 'dict'>
Converting Python to JSON
import json

# Python dictionary
python_dict = {"name": "Bob", "age": 30, "active": True}

# Convert Python object to JSON string
json_output = json.dumps(python_dict)
print(json_output)  # Output: {"name": "Bob", "age": 30, "active": true}
print(type(json_output))  # Output: <class 'str'>

Key Functions

  • json.loads(): Parses a JSON string to a Python object (dictionary or list)
  • json.dumps(): Converts a Python object to a JSON string
  • json.load(): Reads JSON from a file and converts it to Python
  • json.dump(): Writes a Python object as JSON to a file

Remember: The "s" in loads and dumps stands for "string"!

Working with JSON Files
import json

# Reading from a JSON file
with open("data.json", "r") as file:
    data = json.load(file)  # Reads file and parses JSON
    print(data)

# Writing to a JSON file
data_to_save = {"users": ["Alice", "Bob"], "count": 2}
with open("output.json", "w") as file:
    json.dump(data_to_save, file)  # Writes Python object as JSON

JSON vs Python Differences

JSON uses true/false (lowercase) for booleans, while Python uses True/False (capitalized). JSON uses null, Python uses None. The json module handles these conversions automatically!

Nested JSON Structures

JSON can contain nested objects and arrays, just like Python dictionaries and lists:

Complex JSON Example
import json

# Complex JSON with nested structures
json_data = '''
{
  "students": [
    {
      "name": "Alice",
      "grades": [85, 90, 88],
      "info": {
        "age": 20,
        "major": "Computer Science"
      }
    },
    {
      "name": "Bob",
      "grades": [92, 87, 95],
      "info": {
        "age": 21,
        "major": "Mathematics"
      }
    }
  ]
}
'''

data = json.loads(json_data)
print(data["students"][0]["name"])           # Output: Alice
print(data["students"][0]["grades"][0])      # Output: 85
print(data["students"][0]["info"]["major"])  # Output: Computer Science

Accessing Nested Data

When JSON is parsed, nested objects become nested dictionaries, and arrays become lists. Access them just like you would in Python!

Practice: Working with JSON

Try It Yourself

Try parsing and creating JSON data:

Press Run to see output

What happened? You parsed a JSON string into a Python dictionary and then converted a Python dictionary back to a JSON string. This is the foundation of working with JSON!

JSON Best Practices

Follow these practices when working with JSON:

Best Practices

  • Validate JSON: Use json.loads() in try/except to catch invalid JSON
  • Pretty printing: Use indent=2 in json.dump() for readable files
  • Handle encoding: JSON files should be UTF-8 encoded
  • Error handling: Always handle JSONDecodeError when parsing
  • Nested data: Access nested structures carefully to avoid KeyError
Safe JSON Parsing
import json

def safe_parse_json(json_string):
    """Safely parse JSON with error handling"""
    try:
        data = json.loads(json_string)
        return data
    except json.JSONDecodeError as e:
        print(f"Invalid JSON: {e}")
        return None
    except Exception as e:
        print(f"Error: {e}")
        return None

# Usage
json_str = '{"name": "Alice", "age": 25}'
data = safe_parse_json(json_string)
if data:
    print(f"Name: {data['name']}")
Pretty JSON Output
import json

data = {
    "users": [
        {"name": "Alice", "age": 25},
        {"name": "Bob", "age": 30}
    ],
    "total": 2
}

# Pretty print with indentation
pretty_json = json.dumps(data, indent=2)
print(pretty_json)

# Save pretty JSON to file
with open('data.json', 'w') as f:
    json.dump(data, f, indent=2)  # indent=2 makes it readable

Summary

In this lesson, you learned:

  • JSON: Lightweight data format for data exchange
  • Data types: Strings, numbers, booleans, objects, arrays, null
  • Parsing: Use json.loads() to convert JSON strings to Python
  • Creating: Use json.dumps() to convert Python to JSON strings
  • Files: Use json.load() and json.dump() for file operations
  • Nested structures: Access nested data like Python dictionaries and lists
  • Best practices: Validate JSON, handle errors, use pretty printing

Remember

JSON is everywhere in modern programming - APIs, configuration files, data storage. Understanding JSON is essential for working with web data! Practice parsing and creating JSON to become comfortable with this format.

End-of-Lesson Exercises

Think about these questions to reinforce what you've learned:

Exercise 1: JSON Basics

What is JSON and why is it commonly used? What are the main data types in JSON?

Exercise 2: JSON Functions

What's the difference between json.loads() and json.load()? When would you use each?