Skip to content
Advertisement

How to handle json query when list object is a timestamp?

i am making a Python script to check my power prices. and i cannot figure out how to handle this data, when the the list object is a timestamp…

in example this query

{
  "2022-07-19T00:00:00+02:00": {
    "NOK_per_kWh": 1.5862,
    "valid_from": "2022-07-19T00:00:00+02:00",
    "valid_to": "2022-07-19T01:00:00+02:00"
  },
  "2022-07-19T01:00:00+02:00": {
    "NOK_per_kWh": 1.5942,
    "valid_from": "2022-07-19T01:00:00+02:00",
    "valid_to": "2022-07-19T02:00:00+02:00"
  },
  "2022-07-19T02:00:00+02:00": {
    "NOK_per_kWh": 2.5051,
    "valid_from": "2022-07-19T02:00:00+02:00",
    "valid_to": "2022-07-19T03:00:00+02:00"
  },
  "2022-07-19T03:00:00+02:00": {
    "NOK_per_kWh": 1.4132,
    "valid_from": "2022-07-19T03:00:00+02:00",
    "valid_to": "2022-07-19T04:00:00+02:00"
  },
  "2022-07-19T04:00:00+02:00": {
    "NOK_per_kWh": 2.7307,
    "valid_from": "2022-07-19T04:00:00+02:00",
    "valid_to": "2022-07-19T05:00:00+02:00"
  }
}

this is what i get from the api, and i would like to know how to handle it.

i have been googling, an i cannot find my solution.

Advertisement

Answer

I suppose that by “handle” you mean to iterate over timestamped objects and access all inner values:

for timestamp in data.keys():
  print(data[timestamp]["NOK_per_kWh"])
}

or

for timestamp, obj in data.items():
  print(timestamp, obj["valid_from"])
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement