Home > Backend Development > Python Tutorial > How to use the json module to convert Python objects to JSON strings in Python 3.x

How to use the json module to convert Python objects to JSON strings in Python 3.x

WBOY
Release: 2023-07-29 19:55:50
Original
1647 people have browsed it

How to use the json module in Python 3.x to convert Python objects into JSON strings

JSON (JavaScript Object Notation) is a common data exchange format that is widely used in front-end and back-end data transmission and storage . Python provides the json module for processing JSON data. In this article, we will learn how to convert Python objects into JSON strings using the json module.

Use the json.dumps() method to convert Python objects to JSON strings
To convert Python objects to JSON strings, we can use the json.dumps() method. The dumps() method accepts a Python object as a parameter and returns the corresponding JSON string.

The following is an example to convert a Python dictionary object into a JSON string:

import json

person = {
    "name": "Lucy",
    "age": 25,
    "city": "New York"
}

json_str = json.dumps(person)
print(json_str)
Copy after login

The output result is:

{"name": "Lucy", "age": 25, "city": "New York"}
Copy after login

We can see that converting the Python dictionary object person When it is a JSON string, the format of the string remains consistent with the dictionary. Key-value pairs are separated by colons, and multiple key-value pairs are separated by commas.

Use json.dump() method to write Python objects to files
If we want to convert Python objects to JSON strings and write them to files, we can use json.dump() method. The dump() method accepts two parameters: the Python object to be converted and the file object to be written.

The following is an example to convert a Python dictionary object into a JSON string and write it to a file:

import json

person = {
    "name": "Lucy",
    "age": 25,
    "city": "New York"
}

with open('person.json', 'w') as json_file:
    json.dump(person, json_file)
Copy after login

After executing the above code, a file named person will be generated. json file, the content is:

{"name": "Lucy", "age": 25, "city": "New York"}
Copy after login

Use the json.loads() method to convert the JSON string into a Python object
Corresponding to the json.dumps() method is the json.loads() method, Used to convert JSON strings to Python objects.

The following is an example to convert a JSON string to a Python dictionary object:

import json

json_str = '{"name": "Lucy", "age": 25, "city": "New York"}'

person = json.loads(json_str)
print(person)
Copy after login

The output result is:

{'name': 'Lucy', 'age': 25, 'city': 'New York'}
Copy after login
Copy after login

As you can see, convert a JSON string to Python After the object, we can access the key-value pairs in it just like operating Python dictionary objects.

Use the json.load() method to read JSON data from the file and convert it into a Python object
If we have a JSON file and want to read its content and convert it into a Python object, we can use json.load() method. The load() method accepts a file object as a parameter.

The following is an example that reads JSON data from a person.json file and converts it to a Python dictionary object:

import json

with open('person.json', 'r') as json_file:
    person = json.load(json_file)
    print(person)
Copy after login

The output is the same as the previous example:

{'name': 'Lucy', 'age': 25, 'city': 'New York'}
Copy after login
Copy after login

We can see that after converting the content read from the JSON file into a Python object, we can access the key-value pairs just like operating a Python dictionary object.

Summary
With the json module, we can easily convert Python objects into JSON strings and write them to files. At the same time, we can also convert JSON strings into Python objects to easily manipulate the data in them. When interacting with front-end and back-end data, using the json module can greatly simplify data processing and transmission.

The above is the detailed content of How to use the json module to convert Python objects to JSON strings in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template