Home > front end >  How to convert python list to JSON array?
How to convert python list to JSON array?

Time:04-24

If I have python list like

pyList=[‘[email protected]’,’[email protected]’]

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

arrayJson=[{“email”:”[email protected]”},{“ email”:”[email protected]”}]

any idea how to do that ?

CodePudding user response:

You can achieve this by using built-in json module

import json

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

CodePudding user response:

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

  • Related