Iterate through json - date order with weather data - json seems messy

Hi,

bit of code below & tried a variety & just can’t really get started
sorry rather long list of 5 days of weather solar data

I basically want it in an array e.g.
‘2023-07-19T12:00’, 202
‘2023-07-19T13:00’, 303
‘2023-07-19T14:00’, 490

I should be able to work out myself later but I will eventually be wanting to add up total solar from 09:00 to 18:00 for each day, for next 5 days, so need to be able to work on the date, as a date & the solar number as number. the json looks a bit back to front.
e.g.
‘2023-07-19’, 2540
‘2023-07-20’, 3260
‘2023-07-21’, 1923

I don’t expect anyone to try & code that but point me in the right direction to load the source data so I can work on that.

send_text = "https://api.open-meteo.com/v1/forecast?latitude=51.15&longitude=-0.81&hourly=direct_radiation&timezone=Europe%2FLondon"

response = requests.get(send_text)
response.json()  

pprint(response.json())
res = response.json()

print (res['hourly']['direct_radiation'])
#WORKS but just radiation

{'elevation': 61.0,
 'generationtime_ms': 0.5190372467041016,
 'hourly': {'direct_radiation': [0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 4.0,
                                 38.0,
                                 18.0,
                                 36.0,
                                 79.0,
                                 201.0,
                                 300.0,
                                 371.0,
                                 424.0,
                                 364.0,
                                 306.0,
                                 312.0,
                                 209.0,
                                 185.0,
                                 86.0,
                                 11.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 3.0,
                                 63.0,
                                 206.0,
                                 361.0,
                                 499.0,
                                 624.0,
                                 440.0,
                                 413.0,
                                 228.0,
                                 131.0,
                                 112.0,
                                 192.0,
                                 180.0,
                                 11.0,
                                 8.0,
                                 1.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 3.0,
                                 69.0,
                                 123.0,
                                 116.0,
                                 180.0,
                                 387.0,
                                 223.0,
                                 92.0,
                                 114.0,
                                 66.0,
                                 45.0,
                                 67.0,
                                 35.0,
                                 19.0,
                                 11.0,
                                 6.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 1.0,
                                 6.0,
                                 5.0,
                                 5.0,
                                 4.0,
                                 6.0,
                                 2.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 2.0,
                                 11.0,
                                 43.0,
                                 110.0,
                                 192.0,
                                 265.0,
                                 322.0,
                                 348.0,
                                 330.0,
                                 276.0,
                                 211.0,
                                 144.0,
                                 82.0,
                                 40.0,
                                 17.0,
                                 4.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 3.0,
                                 16.0,
                                 47.0,
                                 106.0,
                                 215.0,
                                 358.0,
                                 468.0,
                                 525.0,
                                 499.0,
                                 441.0,
                                 369.0,
                                 279.0,
                                 178.0,
                                 80.0,
                                 15.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 0.0,
                                 8.0,
                                 28.0,
                                 64.0,
                                 118.0,
                                 186.0,
                                 249.0,
                                 300.0,
                                 331.0,
                                 324.0,
                                 281.0,
                                 214.0,
                                 134.0,
                                 54.0,
                                 7.0,
                                 0.0,
                                 0.0],
            'time': ['2023-07-19T00:00',
                     '2023-07-19T01:00',
                     '2023-07-19T02:00',
                     '2023-07-19T03:00',
                     '2023-07-19T04:00',
                     '2023-07-19T05:00',
                     '2023-07-19T06:00',
                     '2023-07-19T07:00',
                     '2023-07-19T08:00',
                     '2023-07-19T09:00',
                     '2023-07-19T10:00',
                     '2023-07-19T11:00',
                     '2023-07-19T12:00',
                     '2023-07-19T13:00',
                     '2023-07-19T14:00',
                     '2023-07-19T15:00',
                     '2023-07-19T16:00',
                     '2023-07-19T17:00',
                     '2023-07-19T18:00',
                     '2023-07-19T19:00',
                     '2023-07-19T20:00',
                     '2023-07-19T21:00',
                     '2023-07-19T22:00',
                     '2023-07-19T23:00',
                     '2023-07-20T00:00',
                     '2023-07-20T01:00',
                     '2023-07-20T02:00',
                     '2023-07-20T03:00',
                     '2023-07-20T04:00',
                     '2023-07-20T05:00',
                     '2023-07-20T06:00',
                     '2023-07-20T07:00',
                     '2023-07-20T08:00',
                     '2023-07-20T09:00',
                     '2023-07-20T10:00',
                     '2023-07-20T11:00',
                     '2023-07-20T12:00',
                     '2023-07-20T13:00',
                     '2023-07-20T14:00',
                     '2023-07-20T15:00',
                     '2023-07-20T16:00',
                     '2023-07-20T17:00',
                     '2023-07-20T18:00',
                     '2023-07-20T19:00',
                     '2023-07-20T20:00',
                     '2023-07-20T21:00',
                     '2023-07-20T22:00',
                     '2023-07-20T23:00',
                     '2023-07-21T00:00',
                     '2023-07-21T01:00',
                     '2023-07-21T02:00',
                     '2023-07-21T03:00',
                     '2023-07-21T04:00',
                     '2023-07-21T05:00',
                     '2023-07-21T06:00',
                     '2023-07-21T07:00',
                     '2023-07-21T08:00',
                     '2023-07-21T09:00',
                     '2023-07-21T10:00',
                     '2023-07-21T11:00',
                     '2023-07-21T12:00',
                     '2023-07-21T13:00',
                     '2023-07-21T14:00',
                     '2023-07-21T15:00',
                     '2023-07-21T16:00',
                     '2023-07-21T17:00',
                     '2023-07-21T18:00',
                     '2023-07-21T19:00',
                     '2023-07-21T20:00',
                     '2023-07-21T21:00',
                     '2023-07-21T22:00',
                     '2023-07-21T23:00',
                     '2023-07-22T00:00',
                     '2023-07-22T01:00',
                     '2023-07-22T02:00',
                     '2023-07-22T03:00',
                     '2023-07-22T04:00',
                     '2023-07-22T05:00',
                     '2023-07-22T06:00',
                     '2023-07-22T07:00',
                     '2023-07-22T08:00',
                     '2023-07-22T09:00',
                     '2023-07-22T10:00',
                     '2023-07-22T11:00',
                     '2023-07-22T12:00',
                     '2023-07-22T13:00',
                     '2023-07-22T14:00',
                     '2023-07-22T15:00',
                     '2023-07-22T16:00',
                     '2023-07-22T17:00',
                     '2023-07-22T18:00',
                     '2023-07-22T19:00',
                     '2023-07-22T20:00',
                     '2023-07-22T21:00',
                     '2023-07-22T22:00',
                     '2023-07-22T23:00',
                     '2023-07-23T00:00',
                     '2023-07-23T01:00',
                     '2023-07-23T02:00',
                     '2023-07-23T03:00',
                     '2023-07-23T04:00',
                     '2023-07-23T05:00',
                     '2023-07-23T06:00',
                     '2023-07-23T07:00',
                     '2023-07-23T08:00',
                     '2023-07-23T09:00',
                     '2023-07-23T10:00',
                     '2023-07-23T11:00',
                     '2023-07-23T12:00',
                     '2023-07-23T13:00',
                     '2023-07-23T14:00',
                     '2023-07-23T15:00',
                     '2023-07-23T16:00',
                     '2023-07-23T17:00',
                     '2023-07-23T18:00',
                     '2023-07-23T19:00',
                     '2023-07-23T20:00',
                     '2023-07-23T21:00',
                     '2023-07-23T22:00',
                     '2023-07-23T23:00',
                     '2023-07-24T00:00',
                     '2023-07-24T01:00',
                     '2023-07-24T02:00',
                     '2023-07-24T03:00',
                     '2023-07-24T04:00',
                     '2023-07-24T05:00',
                     '2023-07-24T06:00',
                     '2023-07-24T07:00',
                     '2023-07-24T08:00',
                     '2023-07-24T09:00',
                     '2023-07-24T10:00',
                     '2023-07-24T11:00',
                     '2023-07-24T12:00',
                     '2023-07-24T13:00',
                     '2023-07-24T14:00',
                     '2023-07-24T15:00',
                     '2023-07-24T16:00',
                     '2023-07-24T17:00',
                     '2023-07-24T18:00',
                     '2023-07-24T19:00',
                     '2023-07-24T20:00',
                     '2023-07-24T21:00',
                     '2023-07-24T22:00',
                     '2023-07-24T23:00',
                     '2023-07-25T00:00',
                     '2023-07-25T01:00',
                     '2023-07-25T02:00',
                     '2023-07-25T03:00',
                     '2023-07-25T04:00',
                     '2023-07-25T05:00',
                     '2023-07-25T06:00',
                     '2023-07-25T07:00',
                     '2023-07-25T08:00',
                     '2023-07-25T09:00',
                     '2023-07-25T10:00',
                     '2023-07-25T11:00',
                     '2023-07-25T12:00',
                     '2023-07-25T13:00',
                     '2023-07-25T14:00',
                     '2023-07-25T15:00',
                     '2023-07-25T16:00',
                     '2023-07-25T17:00',
                     '2023-07-25T18:00',
                     '2023-07-25T19:00',
                     '2023-07-25T20:00',
                     '2023-07-25T21:00',
                     '2023-07-25T22:00',
                     '2023-07-25T23:00']},
 'hourly_units': {'direct_radiation': 'W/m²', 'time': 'iso8601'},
 'latitude': 51.16,
 'longitude': -0.8000002,
 'timezone': 'Europe/London',
 'timezone_abbreviation': 'BST',
 'utc_offset_seconds': 3600}
[]

It looks like there are 2 distinct lists here, 'direct_radiation' and
'time'. I’m assuming they’re in the same order (it would be insane not
to be). So try this:

 timed_values = zip(
     res['hourly']['time'],
     res['hourly']['direct_radiation'],
 )

and print that. Better still:

 timed_values = dict(zip(
     res['hourly']['time'],
     res['hourly']['direct_radiation'],
 ))

and maybe even better:

 from datetime import datetime
 timed_values = {
     datetime.fromisoformat(isotime): radiation
     for isotime, radiation in zip(
         res['hourly']['time'],
         res['hourly']['direct_radiation'],
     )
 }

which zips up the arrays, then makes dict where the key is the time
string converted to a datetime object. Note that the datetime is
“naive”, and does not know about timezones. If that matters you could
use the 'utc_offset_seconds' field from the JSON to make them timezone
aware and either use it for the time zone information in the datetime
object, or convert further into “UNIX” timestamps, which are flat
seconds since midnight 1 January 1970 UTC.

Cheers,
Cameron Simpson cs@cskk.id.au

Very many thanks,

You have made it so easy & perhaps I should have known.

I have not been using python for some time now & been doing lots of visual basic (MSAccess DB) and it rather scrambles my mind trying to go back & forth.

I love python & have done quite a lot a while back but needed to reboot my brain in that mode & you have really kick started me.

thanks again