[python] Pull the value of a specific key from json to make it a list

Asked 2 years ago, Updated 2 years ago, 35 views

[
        {
            "A":"C:/user/...",
            "B":2
        },
        {
            "A":"C:/abd/...",
            "B"4
        }
 ]

When the above file is referred to as example.json, I would like to make a list by extracting only the values for key'A'. How can I code in this case? (For example, I didn't write down the path name correctly, but the actual file contains all the data values correctly.)

with open('example.json') as make_json:
       json_data = json.load(make_json)

I read it like this A_value = json_data["A"] causes a TypeError that the index in the list must be integrers, not str.

I'd appreciate your help.

python json

2022-09-20 16:21

1 Answers

A_values = [ e["A"] for e in json_data ]
To do so

deusyo.


2022-09-20 16:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.