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
Mary-Kate OlsenOriginal
2024-12-17 13:19:35881browse

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)

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

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

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!

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