Convert a list to json in Python

You can convert a list to a JSON string.

import json

s = [1, 2, 3, 'a']
j = json.dumps(s)

print(j)  # [1, 2, 3, "a"]
print(type(j))  # <class 'str'>

s is a list and j is a string formatted in JSON, which can be created by json.dumps(). Writing a list in a json file needs that conversion.

import json

s = [1, 2, 3, 'a']
j = json.dumps(s)

with open('a.json', 'w') as f:
    f.write(j)

a.json

[1, 2, 3, "a"]

Python List

Python Tutorial