PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

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

 July 23, 2022     decode, json, python, timestamp     No comments   

Issue

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.


Solution

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"])
}


Answered By - Filip Hanes
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing