parse json in python

To parse JSON data in Python, you can make use of the json module in Python's standard library. Here's an example of how to parse JSON data and manipulate it as a dictionary:

main.py
import json

# example JSON data as a string
json_data = '{"name": "John", "age": 30, "city": "New York"}'

# deserialize the JSON data into a dictionary
data_dict = json.loads(json_data)

# access values in the dictionary
print(data_dict["name"])
print(data_dict["age"])
print(data_dict["city"])

# iterate over key-value pairs in the dictionary
for key, value in data_dict.items():
    print(key, value)
406 chars
17 lines

In the code above, the json.loads() function is used to parse the JSON data and convert it into a Python dictionary. Once the data is in dictionary format, you can access its values and manipulate them in a variety of ways.

gistlibby LogSnag