Python – having trouble selecting single value from json data

Question:

I have the following code from which I want to select a singular piece of data from the JSON.

I have the following code from which I want to select a singular piece of data from the JSON.

    j = {
    "data": [
        {
            "astronomicalDawn": "2023-01-16T04:58:21+00:00",
            "astronomicalDusk": "2023-01-16T17:00:31+00:00",
            "civilDawn": "2023-01-16T06:38:18+00:00",
            "civilDusk": "2023-01-16T15:20:34+00:00",
            "moonFraction": 0.36248449454701365,
            "moonPhase": {
                "closest": {
                    "text": "Third quarter",
                    "time": "2023-01-14T22:34:00+00:00",
                    "value": 0.75
                },
                "current": {
                    "text": "Waning crescent",
                    "time": "2023-01-16T06:00:00+00:00",
                    "value": 0.7943440617174506
                }
            },
            "moonrise": "2023-01-16T01:01:55+00:00",
            "moonset": "2023-01-16T09:53:57+00:00",
            "nauticalDawn": "2023-01-16T05:46:36+00:00",
            "nauticalDusk": "2023-01-16T16:12:16+00:00",
            "sunrise": "2023-01-16T07:28:07+00:00",
            "sunset": "2023-01-16T14:30:45+00:00",
            "time": "2023-01-16T06:00:00+00:00"
        },
        {
            "astronomicalDawn": "2023-01-17T04:57:26+00:00",
            "astronomicalDusk": "2023-01-17T17:02:07+00:00",
            "civilDawn": "2023-01-17T06:37:07+00:00",
            "civilDusk": "2023-01-17T15:22:26+00:00",
            "moonFraction": 0.26001046334874545,
            "moonPhase": {
                "closest": {
                    "text": "Third quarter",
                    "time": "2023-01-14T21:31:00+00:00",
                    "value": 0.75
                },
                "current": {
                    "text": "Waning crescent",
                    "time": "2023-01-17T06:00:00+00:00",
                    "value": 0.8296778757434323
                }
            },
            "moonrise": "2023-01-17T02:38:30+00:00",
            "moonset": "2023-01-17T10:01:03+00:00",
            "nauticalDawn": "2023-01-17T05:45:35+00:00",
            "nauticalDusk": "2023-01-17T16:13:58+00:00",
            "sunrise": "2023-01-17T07:26:40+00:00",
            "sunset": "2023-01-17T14:32:54+00:00",
            "time": "2023-01-17T06:00:00+00:00"
        }
    ],
    "meta": {
        "cost": 1,
        "dailyQuota": 10,
        "lat": 58.7984,
        "lng": 17.8081,
        "requestCount": 1,
        "start": "2023-01-16 06:00"
    }
}

print(j['data']['moonPhase'])

Which gives me this error;

TypeError: list indices must be integers or slices, not str

That error is in regard to the very last line of the code. But changing the very very last line to print(j['data']) works.

What am I doing wrong – I am trying to select moonPhase data. It turns me on. Thank you.

Asked By: RolandBell

||

Answers:

Try:

print(j['data'][0]['moonPhase'])

or

print(j['data'][1]['moonPhase'])

Explanation: The data property of the json object contains a list. There are two items in the list (item 0 and item 1). You must first select an item using [0] or [1] before selecting the moonPhase property of one of the objects in the list.

edit: If you want to select only items where the moonPhase is in the future try:

print([
    item['moonPhase']
    for item in j['data']
    if (
        datetime.fromisoformat(
            item['moonPhase']['current']['time']
        ).timestamp() > 
        datetime.now().timestamp()
    )
])

output

[{'closest': {'text': 'Third quarter', 'time': '2023-01-14T21:31:00+00:00', 'value': 0.75}, 'current': {'text': 'Waning crescent', 'time': '2023-01-17T06:00:00+00:00', 'value': 0.8296778757434323}}]
Answered By: Zach Flanders
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.