How to generate a list of JSON using Python?

To generate a JSON list, you can use the dumps() function in Python’s json module. Here is an example:

import json

data = [
    {"name": "Alice", "age": 25, "city": "New York"},
    {"name": "Bob", "age": 30, "city": "London"},
    {"name": "Charlie", "age": 35, "city": "Paris"}
]

json_data = json.dumps(data)
print(json_data)

Output:

[{"name": "Alice", "age": 25, "city": "New York"}, {"name": "Bob", "age": 30, "city": "London"}, {"name": "Charlie", "age": 35, "city": "Paris"}]

In this example, we created a list containing dictionaries. We then used the dumps() function to convert the list into a JSON formatted string. Finally, we used the print() function to display the JSON string.

bannerAds