4
{
    "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

3
  • d = json.loads(your_json); for k, v in d.items(): del v['id']; d[k] = v Commented Nov 27, 2020 at 19:22
  • Do you want to remove those items from the actual json file? Commented Nov 27, 2020 at 19:29
  • 1
    Does this answer your question? Delete an element in a JSON object Commented Nov 27, 2020 at 19:31

2 Answers 2

4

Assuming, the file in which you have your JSON is called file.json:

import json
with open('file.json') as f:
    d = json.load(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.load(f)
    for key, value in d.items():
        value.pop('id', None) // this will not crash if the element has no key 'id'
Sign up to request clarification or add additional context in comments.

Comments

2
import json


with open('file.json') as fin:
    your_structure = json.load(fin)

for value in your_structure.values():
    value.pop('id', None)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.