0

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.

AlexK
  • 2,855
  • 9
  • 16
  • 27
  • 1
    Does this answer your question? [How to overcome "datetime.datetime not JSON serializable"?](https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable) – codingatty Jul 19 '22 at 23:20

2 Answers2

1

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"])
}
Filip Hanes
  • 645
  • 4
  • 10
  • Thank you, this was part of what i needed :) i did not know the python would recognize that it actually was a timestamp. but it worked pretty well :) – Vebjørn Endresen Jul 20 '22 at 20:01
1

Really depends on what you mean by "handle" it. This is a weird format where everything after the "+" indicates the local time offset from standard time (Norway is +2 hrs from the UST), so depends on whether you want to extract the dates in UST or Norway's local timezone.

If just UST, you can cut everything after the "+" off, like so:

from datetime import datetime

def extract_ust(t):
    return datetime.strptime(t.split("+")[0], "%Y-%m-%dT%H:%M:%S")

If you want local Norway time, you can do something like:

from datetime import datetime, timedelta

def extract_local(t):
    ust = datetime.strptime(t.split("+")[0], "%Y-%m-%dT%H:%M:%S")
    o_dt = datetime.strptime(t.split("+")[1], "%H:%M")
    offset = timedelta(hours=o_dt.hour, minutes=o_dt.minute)
    return ust + offset

If you're trying to extract the NOK_per_kWh for a given datetime in local Norway time, you can use something like:

def extract_price(returned_json, t=None, time_format="%Y-%m-%dT%H:%M:%S"):
    if t:
        search_t = datetime.strptime(t, time_format)
    else:
        search_t = datetime.now()

    for entry in returned_json.values():
        if extract_local(entry["valid_from"]) <= search_t:
            if search_t < extract_local(entry["valid_to"]):
                return entry["NOK_per_kWh"]

The same function above can be used for UST input by substituting extract_local() for extract_ust(). The above assumes the input time t is in a standard format with the "+" at the end.