Reputation: 200
I would like to convert a Python List to Json, my python list is set as key -> value. However all my values must be sometimes key and sometimes value in my json.
For example :
import json
raw = [
{
"name": "User-Agent",
"value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36"
},
{
"name": "Accept",
"value": "*/*"
},
{
"name": "Accept-Language",
"value": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"
},
{
"name": "Accept-Encoding",
"value": "gzip, deflate, br"
},
{
"name": "Content-Type",
"value": "text/plain;charset=UTF-8"
},
{
"name": "Content-Length",
"value": "205"
},
{
"name": "Connection",
"value": "keep-alive"
}
]
for i in raw:
print (i['name'],i['value'])
I would like something like this in Json format
{
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
'Accept' : '*/*',
'Accept-Language' : 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding' : 'gzip, deflate, br',
'Content-Type' : 'text/plain;charset=UTF-8',
'Content-Length' : '205',
'Connection' : 'keep-alive'
}
Thanks
Upvotes: 1
Views: 107
Reputation: 7206
import json
dict = {}
for item in raw:
dict.update({item['name']:item['value']})
#dict = {item['name'] : item['value'] for item in raw}
print (dict)
print (json.dumps(dict)) # convert dictionary into json
output:
{'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36', 'Accept': '*/*', 'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'text/plain;charset=UTF-8', 'Content-Length': '205', 'Connection': 'keep-alive'}
{"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36", "Accept": "*/*", "Accept-Language": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3", "Accept-Encoding": "gzip, deflate, br", "Content-Type": "text/plain;charset=UTF-8", "Content-Length": "205", "Connection": "keep-alive"}
Upvotes: 0
Reputation: 3346
you can try below code:
d1 = {item['name']:item['value'] for item in raw}
print(d1)
output:
{
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
'Accept': '*/*',
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'text/plain;charset=UTF-8',
'Content-Length': '205',
'Connection': 'keep-alive'
}
Upvotes: 0
Reputation: 5785
Try this,
>>> {e['name']:e['value'] for e in raw}
Output:
{'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
'Accept': '*/*',
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'text/plain;charset=UTF-8',
'Content-Length': '205',
'Connection': 'keep-alive'}
Upvotes: 3
Reputation: 6920
Try this :
dict_ = {}
for i in raw:
dict_[i['name']] = i['value']
or, simply :
dict_ = {i['name'] : i['value'] for i in raw}
Output :
{
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
'Accept' : '*/*',
'Accept-Language' : 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding' : 'gzip, deflate, br',
'Content-Type' : 'text/plain;charset=UTF-8',
'Content-Length' : '205',
'Connection' : 'keep-alive'
}
Upvotes: 2