How to Convert Python List to JSON

To convert a Python list into JSON format, import the json package and call the dumps() function, passing the list to parse as the first argument.

 

To demonstrate this, let's change a simple Python list into JSON and print the output.

 

import json

items = ['a','b','c']

output = json.dumps(items)

print(output)
print(type(output))
["a", "b", "c"]
<class 'str'>

 

Essentially the operation above converts the list into a string of data.

 

Convert a List of Dictionaries to JSON

We can convert a list of dictionaries into JSON, using the same method as above.

 

import json

items = [{'a':1, 'b':2}, {'c':3, 'd':4}]

output = json.dumps(items)

print(output)
[{"a": 1, "b": 2}, {"c": 3, "d": 4}]