I have a json in the following format and I just want to save 2 parts of it. But I get it as separate characters. Can you please help me?
for data in results['results']:
dictionary = dict(zip("title:", data['title']))
dictionary = dict(zip("uuid:", data['uuid']))
Json
results": [
{
"title": "subject1",
"ja_pub_yyyy": 1395,
"uuid": "86ae6",
"external_link": null,
"fulltext_status": 1,
},
{
"title": "subject2",
"ja_pub_yyyy": 1395,
"uuid": "86ae6",
"external_link": null,
"fulltext_status": 1,
}
]
new_result
results": [
{
"title": "subject1",
"id": "86ae6",
},
{
"title": "subject2",
"id": "86ae6",
}]
CodePudding user response:
You are not creating each dict
properly. It is more simple:
new_result = []
for data in results['results']:
l = ((k,data[k]) for k in ("title","uuid"))
dictionary = dict(l)
new_result.append(dictionary)
I have demonstrated collecting each dictionary
.
CodePudding user response:
you can do it this way too
result = [{'title':r['title'],'uuid':r['uuid']} for r in results]
CodePudding user response:
Try to loop over the json and extract the object property into the new array. eg:
results= [
{
"title": "subject1",
"ja_pub_yyyy": 1395,
"uuid": "86ae6",
"external_link": None,
"fulltext_status": 1,
},
{
"title": "subject1",
"ja_pub_yyyy": 1395,
"uuid": "86ae6",
"external_link": None,
"fulltext_status": 1,
}
]
res = []
for data in results:
res.append({'name': data['title'], 'id': data['uuid']})
print(res)