Extract specific data from [Python] json format data

Asked 2 years ago, Updated 2 years ago, 15 views

I have a question because it is difficult to extract the desired value when I receive the json format data. : : )

Example:

[
    {
        "date": "2020-12-26",
        "year": 2020,
        "quarter": 1,
        "class": {
            "name": [
                {
                    "code": "Youngsoo",
                    "values": 15
                },
                {
                    "Code": "Hee Min",
                    "values": 16
                }
            ]
        }
    }
]

If the code value is "Youngsoo", we would like to extract and values from the data. I'm asking you how you can do it.

python

2022-09-20 17:39

1 Answers

You can just look at each other, but I'm not sure what you're curious about.

t = """
[
    {
        "date": "2020-12-26",
        "year": 2020,
        "quarter": 1,
        "class": {
            "name": [
                {
                    "code": "Youngsoo",
                    "values": 15
                },
                {
                    "Code": "Hee Min",
                    "values": 16
                }
            ]
        }
    }
]
"""
import json

d = json.loads(t)
print(d)
print(d[0])
print(d[0]["class"]["name"])
print ([cv for cvind[0]["class"]["name"] if cv["code"] == "accept"])
t = """
[
    {
        "date": "2020-12-26",
        "year": 2020,
        "quarter": 1,
        "class": {
            "name": [
                {
                    "code": "Youngsoo",
                    "values": 15
                },
                {
                    "Code": "Hee Min",
                    "values": 16
                }
            ]
        }
    },
    {
        "date": "2020-12-26",
        "year": 2020,
        "quarter": 1,
        "class": {
            "name": [
                {
                    "code": "Youngsoo",
                    "values": 15
                },
                {
                    "Code": "Hee Min",
                    "values": 16
                }
            ]
        }
    }
]
"""
import json

d = json.loads(t)
print(d)
print([e for e in d])
print([ e["class"]["name"] for e in d])
print ([cv for end for cv in e["class"]["name"] if cv["code"] == "accept"])


2022-09-20 17:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.