How to convert python list to JSON array?

If I have python list like

pyList=[‘x@x.x’,’y@y.y’]

And I want it to convert it to json array and add {} around every object, it should be like that :

arrayJson=[{“email”:”x@x.x”},{“ email”:”y@y.y”}]

any idea how to do that ?

You can achieve this by using built-in json module

import json

arrayJson = json.dumps([{"email": item} for item in pyList])

Try to Google this kind of stuff first. :)

import json

array = [1, 2, 3]
jsonArray = json.dumps(array)

By the way, the result you asked for can not be achieved with the list you provided.

You need to use python dictionaries to get json objects. The conversion is like below

Python -> JSON
list -> array
dictionary -> object

And here is the link to the docs https://docs.python.org/3/library/json.html

pip install jsonwhatever.

You should try it, you can put anything on it

from jsonwhatever import jsonwhatever as jw

pyList=['x@x.x','y@y.y']

jsonwe = jw.JsonWhatEver()

mytr = jsonwe.jsonwhatever('my_custom_list', pyList)

print(mytr)
Back to Top