Home > Backend Development > Python Tutorial > How to Parse JSON Data in Python Using the `json` Module?

How to Parse JSON Data in Python Using the `json` Module?

Mary-Kate Olsen
Release: 2024-12-17 13:19:35
Original
886 people have browsed it

How to Parse JSON Data in Python Using the `json` Module?

Parsing JSON Data in Python

In Python, parsing JSON data involves using the json module. If you have JSON data in a string, you can use the json.loads method to convert it into a Python dictionary:

import json

json_str = '{ "one": "1", "two": "2", "three": "3" }'
data = json.loads(json_str)
Copy after login

Once you have the JSON data as a dictionary, you can access individual values using the key names as shown below:

result = data['two']
print(result)  # Output: 2
Copy after login

Example:

In the example provided, we have a JSON string jsonStr and an input string key as "two". To retrieve the corresponding value, you can use the following code:

import json

json_str = '{ "one": "1", "two": "2", "three": "3" }'
key = "two"
data = json.loads(json_str)
result = data[key]
print(result)  # Output: 2
Copy after login

The above is the detailed content of How to Parse JSON Data in Python Using the `json` Module?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template