Home > Net >  python: Extract all specific subkeys from JSON
python: Extract all specific subkeys from JSON

Time:11-24

If I have JSON similar to as follows:

"children": [
        {
            "firstName": "Alice",
            "age": 6
        },
        {
            "firstName": "Bob",
            "age": 8
        }
        {
            "firstName": "Candice",
            "age": 4
        }
    ]

And I want an array of all firstName values, how would I go about getting them with python?

I have tried the following and it works, but my data set is on the larger side (40,000 lines) and it would be impractical to repeat it:

children = JSON

firstNames = []

firstNames.append(json.loads(children.text)['children'][0]['firstName'])
firstNames.append(json.loads(children.text)['children'][1]['firstName'])
firstNames.append(json.loads(children.text)['children'][2]['firstName'])

I have considered using a for loop to replace the numbers but have no idea how to go about doing so.

CodePudding user response:

@asher, you can significantly improve performance if you run de-serialization only once. And to collect the names you can use the list comprehension:

doc = json.loads(children.text)
firstNames = [child["firstname"] for child in doc["children"]]

CodePudding user response:

It should be clear how to extend what you have. For gosh sakes, don't repeat the JSON load multiple times.

data = json.loads(children.text)
firstNames = [k['firstname'] for k in data['children']]
  • Related