Working with JSON in Python: A Beginner’s Guide

Author:

Working with JSON in Python: A Beginner’s Guide

If you’re just starting with Python, you might have heard about JSON, which stands for JavaScript Object Notation. JSON is a lightweight data-interchange format that is easy for humans to read and write and for machines to parse and generate. Working with JSON in Python is a common task, and it’s essential to understand how to store, retrieve, and manipulate JSON data. In this blog, we’ll explore the basics of working with JSON in Python, covering everything from storing data in a file to encoding custom objects.

What is Python?

Python is a versatile and beginner-friendly programming language that is widely used for various applications, from web development to data analysis and machine learning. It emphasises readability and simplicity, making it an excellent choice for beginners. Python has a vast standard library, which includes modules for handling JSON data seamlessly.

How to Store JSON Data in a File

One of the primary use cases for working with JSON in Python is storing data in a file. This is particularly useful when you need to save the state of your program or share data with other applications. To store JSON data in a file, you can use the `json` module, which is part of the Python standard library.

Let’s consider a simple example where we have a Python dictionary representing a person, and we want to store this data in a JSON file:

import json

# Sample data
person = {
    "name": "Jerry Doe",
    "age": 29,
    "city": "Berlin"
}

# Writing JSON to a file
with open('person.json', 'w') as file:
    json.dump(person, file)

print("JSON data has been stored in 'person.json'")

In this example, the `json.dump()` function is used to write the Python dictionary (`person`) to a file named ‘person.json’. The `’w’` parameter in the `open()` function signifies that we are opening the file for writing.

How to Retrieve Data from a JSON File

Once you’ve stored JSON data in a file, you might want to retrieve and use that data in your Python program. The `json` module provides the `json.load()` function for this purpose. Let’s see how you can load data from a JSON file:

import json

# Reading JSON from a file
with open('person.json', 'r') as file:
    loaded_person = json.load(file)

# Displaying the loaded data
print("Loaded JSON data:")
print(loaded_person)

Here, the `json.load()` function reads the content of the ‘person.json’ file and returns a Python object representing the JSON data. In this case, it will be a dictionary (`loaded_person`). You can then manipulate or display the loaded data as needed.

How to Format the JSON Output

When working with JSON data, readability is crucial. The `json` module provides options for formatting the output to make it more human-readable. The `indent` parameter in the `json.dump()` function specifies the number of spaces to use for indentation.

Let’s modify the previous example to include indentation:

import json

# Sample data
person = {
    "name": "Jerry Doe",
    "age": 29,
    "city": "Berlin"
}

# Writing JSON to a file with indentation
with open('person_indented.json', 'w') as file:
    json.dump(person, file, indent=4)

print("Indented JSON data has been stored in 'person_indented.json'")

In this example, the JSON data will be written to ‘person_indented.json’ with an indentation of 4 spaces, making it more readable.

The json.tool Command Line Tool

Python also provides a command-line tool for working with JSON, called `json.tool`. This tool is useful for formatting JSON data directly from the command line. You can use it to validate and pretty-print JSON data without writing a Python script.

Here’s an example of using `json.tool` from the command line:

$ cat person.json | python -m json.tool

In this command, the `cat` command is used to display the contents of ‘person.json’, and the output is then piped (`|`) to `python -m json.tool`, which formats and prints the JSON data.

This use of Bash in the example demonstrates an alternative method for formatting and displaying JSON data without writing a separate Python script. It illustrates how Python can be integrated with command-line tools like `json.tool` to perform specific tasks directly from the command line.

JSON Encoding Custom Objects

While the `json` module can handle basic Python types (e.g., dictionaries, lists, strings, numbers), it may encounter difficulties when trying to encode custom objects. To address this, you can define a custom encoding function using the `default` parameter of the `json.dump()` function.

Let’s consider an example where we have a custom class:

import json

class Person:
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city

# Define a custom encoding function
def encode_person(obj):
    if isinstance(obj, Person):
        return {'name': obj.name, 'age': obj.age, 'city': obj.city}
    return obj

# Create an instance of the custom class
person_object = Person("Jerry Doe", 29, "Berlin")

# Writing JSON to a file with custom encoding
with open('custom_person.json', 'w') as file:
    json.dump(person_object, file, default=encode_person)

print("Custom JSON data has been stored in 'custom_person.json'")

In this example, the `encode_person` function is defined to handle instances of the `Person` class. The `default` parameter of `json.dump()` specifies this custom encoding function.

How to Create JSON from a Python Dictionary

In addition to storing data in a file, you may also need to generate JSON data from a Python dictionary. The `json.dumps()` function can be used for this purpose. The ‘s’ in `dumps` stands for ‘string,’ indicating that it returns a JSON-formatted string.

Here’s an example:

import json

# Sample data
person = {
    "name": "Jerry Doe",
    "age": 29,
    "city": "Berlin"
}

# Create JSON from a Python dictionary
json_data = json.dumps(person)

print("Generated JSON data:")
print(json_data)

In this example, the `json.dumps()` function converts the Python dictionary (`person`) into a JSON-formatted string (`json_data`), which can be printed or used as needed.

Fundamental Skill

Working with JSON in Python is a fundamental skill for any developer. Whether you’re storing data in a file, retrieving it, formatting the output, or dealing with custom objects, the `json` module provides a powerful and user-friendly interface. By mastering these basics, you’ll be well-equipped to handle JSON data in your Python projects.

Become a Full-Stack Developer

Want to find out more about Python? Attend one of our webinars on learning to code with Code Institute’s Full-Stack Software Development programme. Register for an event through the form below.

Coding the Future: Developer Spotlight - Nathan Parsley

In May 2023, Nathan Parsley joined the Level 5 Diploma in Web Application Development programme, delivered at East Kent College Group (Ashford) by Rachel Furlong using the Code Institute learning platform. The course equips learners with full-stack software development skills, preparing them to enter the digital workforce in various roles. Nathan has completed the programme […]

Coding the Future: Developer Spotlight - Wendy Bovill

In March 2023, Wendy Bovill joined the Level 5 Diploma in Web Application Development programme, which is delivered at Westminster Adult Education Service (WAES) using the Code Institute learning platform. The course equips learners with full-stack software development skills, making them ready to enter the digital workforce in various roles. Digital Academy leader Candice Fanning […]

Coding the Future: Developer Spotlight - Ryan McNally

In November 2022, Ryan McNally joined the Level 5 Diploma in Web Application Development programme, delivered at City of Bristol College by Pasquale Fasulo and Ben Smith using the Code Institute learning platform. The course equips learners with full-stack software development skills, preparing them to enter the digital workforce in various roles. We asked Ryan’s […]