{
"duncan_long": {
"id": "drekaner",
"name": "Duncan Long",
"favorite_color": "Blue"
},
"kelsea_head": {
"id": "wagshark",
"name": "Kelsea Head",
"favorite_color": "Ping"
},
"phoenix_knox": {
"id": "jikininer",
"name": "Phoenix Knox",
"favorite_color": "Green"
},
"adina_norton": {
"id": "slimewagner",
"name": "Adina Norton",
"favorite_color": "Red"
}
}
I am trying to Return a JSON list of all the users excluding the id of the user
Assuming your the file in which you have your JSON is called file.json
:
import json
with open('file.json') as f:
d = json.loads(f)
for key, value in d.items():
del value['id']
d[key] = value
Alternative you can use the following:
import json
with open('file.json') as f:
d = json.loads(f)
for key, value in d.items():
value.pop('id', None) // this will not crash if the element has no key 'id'
import json
with open('file.json') as fin:
your_structure = json.load(fin)
for value in your_structure.values():
value.pop('id', None)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.