have a list with values
test = ["mark","will","john"]
using for loop how to convert this into JSON like this
test =[{"name":"mark"},{"name":"john"},{"name":"will"}]
Tried this
for i in test :
print({"name":i})
have a list with values
test = ["mark","will","john"]
using for loop how to convert this into JSON like this
test =[{"name":"mark"},{"name":"john"},{"name":"will"}]
Tried this
for i in test :
print({"name":i})
import json
test =[{"name":"mark"},{"name":"john"},{"name":"will"}]
testJson = json.dumps(test)
print(testJson)
functional way using map()
test = ["mark", "will", "john"]
toJson = lambda _name: {"name": _name} # this expression returns {"name": name} for every iterated "_name"
testJson = map(toJson, test)
for name in testJson:
print(name)
to print a list of a map object cast it to list, example list(testJson)
and you can replace toJson with your lambda expression to customize the result
>>> test = ["mark","will","john"]
>>> final=[]
>>> for i in test:
d=dict()
d['name']=i
final.append(d)
>>> final
>>>[{'name': 'mark'}, {'name': 'will'}, {'name': 'john'}]