Table of Contents
Example: Convert Python objects to JSON format using json.dump()
Example: Convert Python objects to JSON format using json.dumps()
Example: Convert a JSON object to a Python object using json.load()
Example: Convert a JSON object to a Python dictionary using json.loads()
How to parse JSON in Python?
How to convert a Python object to JSON?
How to read JSON from a file in Python?
How to write JSON to a file in Python?
How to print JSON beautifully in Python?
How to deal with complex Python objects in JSON?
How to parse JSON with unknown structure?
How to handle large JSON files in Python?
How to handle JSON errors in Python?
How to use JSON with HTTP requests in Python?
Home Backend Development Python Tutorial Working with JSON Files in Python, with Examples

Working with JSON Files in Python, with Examples

Feb 16, 2025 am 09:47 AM

Working with JSON Files in Python, with Examples

JSON in Python: A comprehensive guide

JSON (JavaScript object notation) is a language-independent data exchange format that is widely used to transfer data between clients and servers. Python supports JSON through multiple modules, among which "json" and "simplejson" are the most popular.

Python's built-in "json" module provides methods to read and write JSON files. The "json.load()" method is used to read JSON data from a file, and the "json.dump()" method is used to write JSON data to a file.

Python objects can be converted to JSON format through the serialization process, using the "json.dump()" or "json.dumps()" methods. Instead, JSON data can be converted back to Python objects through a deserialization process using the "json.load()" or "json.loads()" methods.

Python and JSON data types have equivalents. For example, Python's "dict" is equivalent to JSON's "object", while Python's "list" or "tuple" is equivalent to JSON's "array". This mapping facilitates the conversion process between Python and JSON.

In this tutorial, we will learn how to read, write, and parse JSON in Python using relevant examples. We will also explore common modules in Python for handling JSON.

JSON is a lightweight data exchange format. It is a common format for transferring and receiving data between clients and servers. However, its application and purpose are not limited to the transmission of data. The machine can easily generate and parse JSON data. The JSON acronym stands for JavaScript object notation, as the name suggests, is a subset of the JavaScript programming language.

JSON is a standardized data exchange format and is language-independent. Almost all programming languages ​​support it in some way. It has the following structure:

  • It has a left brace on the left and a right brace on the right.
  • It has a set of name/value pairs.
  • Each name is separated from its value by a colon ":".
  • Each name/value pair is separated by a comma (,).

The following is a fragment of a JSON object:

{
    "name": "Chinedu Obi",
    "age": 24,
    "country": "Nigeria",
    "languages": [
        "Igbo",
        "English",
        "Yoruba"
    ],
    "marital status": "single",
    "employee": true,
    "experience": [
        {
            "title": "Frontend Engineering Intern",
            "company": "Andela"
        },
        {
            "title": "Frontend Engineer",
            "company": "Paystack"
        }
    ]
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

For the purposes of future code examples, we will assume that the above JSON is stored in a file named employee.json.

JSON data type

When using a JSON object, Python converts the JSON data type to its equivalent and vice versa. The following table shows the Python data types and their JSON equivalents.

Python JSON 等效项
dict object
list, tuple array
str string
int, float, long number
True true
False false
None null

The difference between json and simplejson modules in Python

There are several modules in Python for encoding and decoding JSON. The two most popular modules are json and simplejson. The json module is a built-in package in the Python standard library, which means we can use it directly without installing it.

The simplejson module is an external Python module for encoding and decoding JSON. It is an open source package that is backwards compatible with Python 2.5 and Python 3.3. It is also fast, simple, correct and scalable.

simplejson is updated more frequently, with updated optimizations than json, making it faster. If you are using older Python below 2.6 in your legacy project, simplejson is your best bet.

In this tutorial, we will stick to the json module.

How to read and write JSON files in Python

When programming in Python, we often encounter the JSON data format, and it is important to understand how to read or write JSON data and files. Here's a pre-understanding of file processing in Python that will help read and write JSON files.

How to read a JSON file in Python

Like every other read operation in Python, the with statement can be used with the json.load() method to read a JSON file.

See the following code example:

{
    "name": "Chinedu Obi",
    "age": 24,
    "country": "Nigeria",
    "languages": [
        "Igbo",
        "English",
        "Yoruba"
    ],
    "marital status": "single",
    "employee": true,
    "experience": [
        {
            "title": "Frontend Engineering Intern",
            "company": "Andela"
        },
        {
            "title": "Frontend Engineer",
            "company": "Paystack"
        }
    ]
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
The following is the output of the above code:

import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)
Copy after login
Copy after login
Copy after login
Copy after login
In the above code, we open the employee.json file in read mode. The json.load() method decodes the JSON data into a Python dictionary stored in the employee_dict variable.

How to write JSON to a file in Python

We can also write JSON data to files in Python. Like the read operation, we use the with statement and the json.dump() method to write JSON data to a file.

Consider the following code snippet:

<code>{'name': 'Chinedu Obi', 'age': 24, 'country': 'Nigeria', 'languages': ['Igbo', 'English', 'Yoruba'], 'marital status': 'single', 'employee': True, 'experience': [{'title': 'Frontend Engineering Intern', 'company': 'Andela'}, {'title': 'Frontend Engineer', 'company': 'Paystack'}]}</code>
Copy after login
Copy after login
Copy after login
Copy after login
Here, we create a Python dictionary mother with data about the fictional mother. We open mother.json in write mode. Since there is no such file, a file will be created for us. The json.dump() method encodes the Python dictionary assigned to the mother variable as a JSON equivalent, which is written to the specified file. After executing the above code, it will appear in the root directory of our folder, which contains the mother.json file of JSON data.

How to convert a Python dictionary to JSON (serialization)

Serialization is the process of converting a Python object (in most cases a dictionary) to JSON formatted data or strings. When serialized, the Python type is encoded as a JSON equivalent. The json module provides two methods—json.dump() and json.dumps()—for serializing Python objects into JSON format.

    json.dump() is used to write the JSON equivalent of a Python object to a file.
  • json.dumps() (with "s") is used to convert Python objects to strings in JSON format.
Please note the following syntax:

import json

mother = {
    "name": "Asake Babatunde",
    "age": 28,
    "marital status": "Married",
    "children": ["Ayo", "Tolu", "Simi"],
    "staff": False,
    "next of kin": {"name": "Babatune Lanre", "relationship": "husband"},
}

with open("mother.json", "w", encoding="utf-8") as file_handle:
    json.dump(mother, file_handle, indent=4)
Copy after login
Copy after login
Copy after login
Copy after login
The
json.dump(obj, fp, indent)
Copy after login
Copy after login
Copy after login
Copy after login
json.dump() method has a parameter fp, while json.dumps() does not.

Some parameter explanations:

  • obj: A Python object to be serialized into JSON format.
  • fp: File pointer (object) with methods such as read() or write().
  • indent: Non-negative integer or string indicating the indentation level of the beautifully printed JSON data.

Example: Convert Python objects to JSON format using json.dump()

Let's encode the Python object into equivalent JSON formatted data and write it to a file.

First, we create a Python dictionary:

{
    "name": "Chinedu Obi",
    "age": 24,
    "country": "Nigeria",
    "languages": [
        "Igbo",
        "English",
        "Yoruba"
    ],
    "marital status": "single",
    "employee": true,
    "experience": [
        {
            "title": "Frontend Engineering Intern",
            "company": "Andela"
        },
        {
            "title": "Frontend Engineer",
            "company": "Paystack"
        }
    ]
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Let's encode our dictionary as JSON data and write to a file:

import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)
Copy after login
Copy after login
Copy after login
Copy after login

In the example above, we pass the dictionary, file pointer, and indent parameters to the json.dump method. Here is the output of our code. After executing the code, the subject.json file containing the expected JSON data will be found in our project root folder:

<code>{'name': 'Chinedu Obi', 'age': 24, 'country': 'Nigeria', 'languages': ['Igbo', 'English', 'Yoruba'], 'marital status': 'single', 'employee': True, 'experience': [{'title': 'Frontend Engineering Intern', 'company': 'Andela'}, {'title': 'Frontend Engineer', 'company': 'Paystack'}]}</code>
Copy after login
Copy after login
Copy after login
Copy after login

Our output has a nice printout because we added an indent parameter with a value of 4.

Example: Convert Python objects to JSON format using json.dumps()

In this example, we encode the Python object as a JSON string. We created a subject dictionary before, so we can reuse it here.

Let's take the json.dumps() method as an example:

import json

mother = {
    "name": "Asake Babatunde",
    "age": 28,
    "marital status": "Married",
    "children": ["Ayo", "Tolu", "Simi"],
    "staff": False,
    "next of kin": {"name": "Babatune Lanre", "relationship": "husband"},
}

with open("mother.json", "w", encoding="utf-8") as file_handle:
    json.dump(mother, file_handle, indent=4)
Copy after login
Copy after login
Copy after login
Copy after login

The following is the output of the above code:

json.dump(obj, fp, indent)
Copy after login
Copy after login
Copy after login
Copy after login

As mentioned earlier, the json.dumps() method is used to convert Python objects to strings in JSON format. We can see from the console that our JSON data has type str.

How to convert JSON to Python dictionary (deserialization)

The deserialization of JSON is to decode a JSON object into an equivalent Python object or Python type. We can use two methods provided by the json module - json.load() and json.loads() - to convert JSON formatted data into Python objects.

  • json.load() is used to read JSON formatted data from a file.
  • json.loads() (with "s") is used to parse JSON strings into Python dictionaries.

Please note the following syntax:

json.dumps(obj, indent)
Copy after login
Copy after login
import json

subject = {
    "name": "Biology",
    "teacher": {"name": "Nana Ama", "sex": "female"},
    "students_size": 24,
    "elective": True,
    "lesson days": ["Tuesday", "Friday"],
}
Copy after login
Copy after login

json.dump() has a parameter fp, and json.dumps() has a parameter s. Other parameters remain unchanged.

Some parameter explanations:

  • fp: File pointer (object) with methods such as read() or write().
  • s: A str, bytes, or bytearray instance containing a JSON document.

Example: Convert a JSON object to a Python object using json.load()

The following is the contents of a new JSON file named students.json:

with open('subject.json', 'w', encoding='utf-8') as file_handle:
    json.dump(subject, file_handle, indent=4)
Copy after login

In this example, we will decode the JSON data from the students.json file to a Python object:

{
    "name": "Biology",
    "teacher": {
        "name": "Nana Ama",
        "sex": "female"
    },
    "students_size": 24,
    "elective": true,
    "lesson days": [
        "Tuesday",
        "Friday"
    ]
}
Copy after login

The following is the output of the above code:

json_data = json.dumps(subject, indent=4)
print(json_data)
print(type(json_data))
Copy after login

In the above code snippet, a JSON file containing the student list is being parsed. JSON data from the file_handle file is passed to the json.load() method, which decodes it into a list of Python dictionaries. Then print the list item to the console.

Example: Convert a JSON object to a Python dictionary using json.loads()

In this example, let's decode JSON data from the API endpoint from the JSONPlaceholder. Before continuing with this example, the requests module should be installed:

{
    "name": "Chinedu Obi",
    "age": 24,
    "country": "Nigeria",
    "languages": [
        "Igbo",
        "English",
        "Yoruba"
    ],
    "marital status": "single",
    "employee": true,
    "experience": [
        {
            "title": "Frontend Engineering Intern",
            "company": "Andela"
        },
        {
            "title": "Frontend Engineer",
            "company": "Paystack"
        }
    ]
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

The following is the output of the above code:

import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)
Copy after login
Copy after login
Copy after login
Copy after login

In the Python code above, we get the response from the endpoint that returns the JSON formatted string. We pass the response as a parameter to the json.loads() method, decode it into a Python dictionary.

Conclusion

In modern web development, JSON is the actual format for exchanging data between a server and a web application. Today, REST API endpoints return data in JSON format, so it is important to understand how to use JSON.

Python has modules such as json and simplejson for reading, writing, and parsing JSON. The json module is provided with the Python standard library, and simplejson is an external package that must be installed before use.

When building a RESTful API in Python or using an external API in our project, we often need to serialize Python objects to JSON and deserialize them back to Python. The methods demonstrated in this article are used by many popular projects. The steps are usually the same.

(The code for this tutorial can be found on GitHub.)

FAQs about JSON in Python (FAQ)

How to parse JSON in Python?

Due to the json module, parsing JSON in Python is a simple process. You can parse JSON strings using the json.loads() function. Here is an example:

<code>{'name': 'Chinedu Obi', 'age': 24, 'country': 'Nigeria', 'languages': ['Igbo', 'English', 'Yoruba'], 'marital status': 'single', 'employee': True, 'experience': [{'title': 'Frontend Engineering Intern', 'company': 'Andela'}, {'title': 'Frontend Engineer', 'company': 'Paystack'}]}</code>
Copy after login
Copy after login
Copy after login
Copy after login

In this code, json.loads() converts the JSON string to a Python dictionary, and you can then interact with it like you would with any other dictionary.

How to convert a Python object to JSON?

The

json module provides the json.dumps() function, which converts Python objects into JSON strings. Here is an example:

import json

mother = {
    "name": "Asake Babatunde",
    "age": 28,
    "marital status": "Married",
    "children": ["Ayo", "Tolu", "Simi"],
    "staff": False,
    "next of kin": {"name": "Babatune Lanre", "relationship": "husband"},
}

with open("mother.json", "w", encoding="utf-8") as file_handle:
    json.dump(mother, file_handle, indent=4)
Copy after login
Copy after login
Copy after login
Copy after login

In this code, json.dumps() converts the Python dictionary to a JSON string.

How to read JSON from a file in Python?

You can use the json.load() function to read JSON data from a file. Here is an example:

json.dump(obj, fp, indent)
Copy after login
Copy after login
Copy after login
Copy after login

In this code, json.load() reads the file and converts the JSON data into a Python object.

How to write JSON to a file in Python?

You can use the json.dump() function to write JSON data to a file. Here is an example:

json.dumps(obj, indent)
Copy after login
Copy after login

In this code, json.dump() writes a Python object to a file as JSON data.

How to print JSON beautifully in Python?

The

json.dumps() function provides the option to print JSON beautifully. Here is an example:

import json

subject = {
    "name": "Biology",
    "teacher": {"name": "Nana Ama", "sex": "female"},
    "students_size": 24,
    "elective": True,
    "lesson days": ["Tuesday", "Friday"],
}
Copy after login
Copy after login

In this code, the indent parameter specifies how many spaces to be used as indent, which makes the output easier to read.

How to deal with complex Python objects in JSON?

The json module can only handle simple Python objects by default. For complex objects like custom classes, you need to provide a function to tell it how to serialize the objects. Here is an example:

{
    "name": "Chinedu Obi",
    "age": 24,
    "country": "Nigeria",
    "languages": [
        "Igbo",
        "English",
        "Yoruba"
    ],
    "marital status": "single",
    "employee": true,
    "experience": [
        {
            "title": "Frontend Engineering Intern",
            "company": "Andela"
        },
        {
            "title": "Frontend Engineer",
            "company": "Paystack"
        }
    ]
}
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

In this code, the encode_person function is used to convert Person objects into serializable formats.

How to parse JSON with unknown structure?

If you don't know the structure of JSON data in advance, you can still parse it into a Python object and explore it. Here is an example:

import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)
Copy after login
Copy after login
Copy after login
Copy after login

In this code, json.loads() converts the JSON string to a Python dictionary, which you can then iterate over to explore its contents.

How to handle large JSON files in Python?

For large JSON files, you can use the ijson package that allows you to stream JSON data instead of loading all of the data into memory at once. Here is an example:

<code>{'name': 'Chinedu Obi', 'age': 24, 'country': 'Nigeria', 'languages': ['Igbo', 'English', 'Yoruba'], 'marital status': 'single', 'employee': True, 'experience': [{'title': 'Frontend Engineering Intern', 'company': 'Andela'}, {'title': 'Frontend Engineer', 'company': 'Paystack'}]}</code>
Copy after login
Copy after login
Copy after login
Copy after login

In this code, ijson.items() generates an object stream from JSON data, which you can then iterate over.

How to handle JSON errors in Python?

When the json module encounters an invalid JSON, it raises a JSONDecodeError. You can catch this error and handle it properly. Here is an example:

import json

mother = {
    "name": "Asake Babatunde",
    "age": 28,
    "marital status": "Married",
    "children": ["Ayo", "Tolu", "Simi"],
    "staff": False,
    "next of kin": {"name": "Babatune Lanre", "relationship": "husband"},
}

with open("mother.json", "w", encoding="utf-8") as file_handle:
    json.dump(mother, file_handle, indent=4)
Copy after login
Copy after login
Copy after login
Copy after login

In this code, the try/except block catches the JSONDecodeError and prints the error message.

How to use JSON with HTTP requests in Python?

The

requests library makes it easy to send HTTP requests and process JSON responses using JSON data. Here is an example:

json.dump(obj, fp, indent)
Copy after login
Copy after login
Copy after login
Copy after login

In this code, requests.post() sends a POST request using JSON data, and response.json() parses the JSON response.

The above is the detailed content of Working with JSON Files in Python, with Examples. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles