Home > Backend Development > Python Tutorial > How to format json

How to format json

尊渡假赌尊渡假赌尊渡假赌
Release: 2024-01-22 14:06:22
Original
3555 people have browsed it

You can use the json module in Python to format JSON data, use the json.loads() function to parse the original JSON data, and use the json.dumps() function to format the parsed data with indentation. The string can be.

How to format json

#In Python, you can use the json module to format JSON data. Here is an example:

import json

# 原始的JSON数据
json_data = '{"name": "John", "age": 30, "city": "New York"}'

# 解析JSON数据
parsed_data = json.loads(json_data)

# 格式化输出
formatted_data = json.dumps(parsed_data, indent=4)

print(formatted_data)
Copy after login

This will output the formatted JSON data:

{
    "name": "John",
    "age": 30,
    "city": "New York"
}
Copy after login

In the above example, use the json.loads() function to parse the raw JSON data and use The json.dumps() function formats the parsed data into an indented string. By setting the value of the indent parameter to 4, you can specify the number of spaces for indentation.

The above is the detailed content of How to format json. For more information, please follow other related articles on the PHP Chinese website!

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