Home >Backend Development >Python Tutorial >Working with JSON Files in Python, with Examples

Working with JSON Files in Python, with Examples

Christopher Nolan
Christopher NolanOriginal
2025-02-16 09:47:09193browse

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:

<code class="language-json">{
    "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>

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:

<code class="language-json">{
    "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>
The following is the output of the above code:

<code class="language-python">import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)</code>
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>
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:

<code class="language-python">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)</code>
The
<code class="language-python">json.dump(obj, fp, indent)</code>
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:

<code class="language-json">{
    "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>

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

<code class="language-python">import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)</code>

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>

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:

<code class="language-python">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)</code>

The following is the output of the above code:

<code class="language-python">json.dump(obj, fp, indent)</code>

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:

<code class="language-python">json.dumps(obj, indent)</code>
<code class="language-python">import json

subject = {
    "name": "Biology",
    "teacher": {"name": "Nana Ama", "sex": "female"},
    "students_size": 24,
    "elective": True,
    "lesson days": ["Tuesday", "Friday"],
}</code>

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:

<code class="language-python">with open('subject.json', 'w', encoding='utf-8') as file_handle:
    json.dump(subject, file_handle, indent=4)</code>

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

<code class="language-json">{
    "name": "Biology",
    "teacher": {
        "name": "Nana Ama",
        "sex": "female"
    },
    "students_size": 24,
    "elective": true,
    "lesson days": [
        "Tuesday",
        "Friday"
    ]
}</code>

The following is the output of the above code:

<code class="language-python">json_data = json.dumps(subject, indent=4)
print(json_data)
print(type(json_data))</code>

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:

<code class="language-json">{
    "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>

The following is the output of the above code:

<code class="language-python">import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)</code>

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>

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:

<code class="language-python">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)</code>

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:

<code class="language-python">json.dump(obj, fp, indent)</code>

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:

<code class="language-python">json.dumps(obj, indent)</code>

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:

<code class="language-python">import json

subject = {
    "name": "Biology",
    "teacher": {"name": "Nana Ama", "sex": "female"},
    "students_size": 24,
    "elective": True,
    "lesson days": ["Tuesday", "Friday"],
}</code>

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:

<code class="language-json">{
    "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>

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:

<code class="language-python">import json

with open('employee.json', 'r', encoding='utf-8') as file_object:
    employee_dict = json.load(file_object)
    print(employee_dict)</code>

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>

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:

<code class="language-python">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)</code>

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:

<code class="language-python">json.dump(obj, fp, indent)</code>

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:
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