Skip to Content
Course content

6.3.1 Parsing and writing JSON

JSON (JavaScript Object Notation) is widely used for data interchange between systems. Python provides the json module to easily parse JSON data and write data to JSON files. Below, we'll explore how to parse and write JSON data using Python.

1. Parsing JSON

Parsing refers to reading JSON data and converting it into a Python object, such as a dictionary or list.

1.1 Parsing JSON from a String

If you have a JSON string and you want to convert it into a Python object, you can use the json.loads() method.

Example: Parsing JSON String

import json

# JSON string
json_string = '{"name": "John", "age": 30, "city": "New York"}'

# Parse the JSON string into a Python dictionary
data = json.loads(json_string)

# Print the parsed data
print(data)

Output:

{'name': 'John', 'age': 30, 'city': 'New York'}
  • The json.loads() function converts the JSON string into a Python dictionary, where keys are strings and values are of the appropriate Python type (e.g., int, str).

1.2 Parsing JSON from a File

To parse JSON from a file, use the json.load() method. This reads the content of the JSON file and converts it into a Python object.

Example: Parsing JSON from a File

import json

# Open a JSON file for reading
with open('data.json', 'r') as file:
    data = json.load(file)

# Print the parsed data
print(data)
  • The json.load() function will read the content of the file, parse the JSON data, and return it as a Python object (dictionary or list, depending on the structure).

2. Writing JSON

Once you have a Python object, you can write it to a JSON file or convert it into a JSON string. Writing data to a JSON file is useful for data storage, configuration files, or data exchange.

2.1 Writing JSON to a File

To write a Python object to a JSON file, use the json.dump() method. This converts the Python object into a JSON string and writes it to the file.

Example: Writing JSON to a File

import json

# Data to write
data = {
    "name": "Jane",
    "age": 25,
    "city": "San Francisco"
}

# Open a file for writing and store the data in JSON format
with open('output.json', 'w') as file:
    json.dump(data, file, indent=4)

print("Data written to output.json")
  • The json.dump() function writes the Python object (data in this case) to the file.
  • The indent=4 parameter is used to pretty-print the JSON with indentation for readability.

Output in output.json:

{
    "name": "Jane",
    "age": 25,
    "city": "San Francisco"
}

2.2 Converting Python Object to JSON String

If you want to convert a Python object into a JSON string (for example, to send it over the network or save it to a variable), use json.dumps().

Example: Convert Python Object to JSON String

import json

# Data to convert
data = {
    "name": "Alice",
    "age": 28,
    "city": "Los Angeles"
}

# Convert Python object to JSON string
json_string = json.dumps(data, indent=4)

# Print the JSON string
print(json_string)

Output:

{
    "name": "Alice",
    "age": 28,
    "city": "Los Angeles"
}
  • The json.dumps() function returns a string containing the JSON-encoded data.
  • You can use the indent parameter to make the JSON string more readable.

3. Handling Special Cases

3.1 Converting Python Objects to JSON

By default, JSON supports basic data types such as strings, numbers, lists, and dictionaries. However, some Python objects, like custom classes or objects, cannot be directly converted to JSON. In such cases, you can define a custom serialization function.

Example: Custom Serialization

import json

# Define a custom Python object
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Custom function to serialize Person object
def person_serializer(obj):
    if isinstance(obj, Person):
        return {'name': obj.name, 'age': obj.age}
    raise TypeError("Type not serializable")

# Create a Person object
person = Person("John", 40)

# Serialize the object to JSON
json_data = json.dumps(person, default=person_serializer)

print(json_data)

Output:

{"name": "John", "age": 40}
  • The default parameter in json.dumps() specifies a function to handle custom objects during serialization.

3.2 Handling Non-Serializable Objects

To handle objects that are not serializable by default (like sets, complex numbers, etc.), you can provide a custom function that tells Python how to convert them to a JSON-serializable format.

4. Summary

  • json.loads(): Parses a JSON string into a Python object.
  • json.load(): Parses JSON data from a file into a Python object.
  • json.dumps(): Converts a Python object to a JSON string.
  • json.dump(): Writes a Python object to a JSON file.
  • Custom Serialization: You can define custom serialization and deserialization for complex Python objects using the default and object_hook parameters.

By mastering these techniques, you'll be able to easily work with JSON data in Python, whether reading it from a file, writing it to a file, or handling complex data structures.

Commenting is not enabled on this course.