JSON. Again...

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

JSON. Again...

Post by peterbos »

Hi,

Not for the first time I have trouble extracting data from a JSON table. I have this table in a HTTPResponse:

Code: Select all

{
 "Prices": [
  {
   "price": 0.11,
   "readingDate": "2024-01-08T22:00:00Z"
  }
 ],
 "intervalType": 4,
 "average": 0.11,
 "fromDate": "2024-01-08T22:00:00Z",
 "tillDate": "2024-01-08T22:59:59Z"
}
There's only one value in the subtable Prices. How do I extract the value of the field "price"? I tried

Code: Select all

item.json.Prices[1].price
but that returns nil.

Peter
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: JSON. Again...

Post by philchillbill »

item.Prices[0].price would do it, assuming the snippet you showed above is called item
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: JSON. Again...

Post by peterbos »

Hi philchillbill,

That does not work but I found the solution. Before reading the json I have to do some mysterious converting with

Code: Select all

local json = domoticz.utils.fromJSON(item.data)
local Prijs = json.Prices[1].price  -- Prices[1], not Prices[0]

The confusing thing is that I can get the value of (say) the "average" field with item.json.average without the conversion.

Peter
User avatar
philchillbill
Posts: 399
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: JSON. Again...

Post by philchillbill »

Sorry, my answer assumed normal json parsing whereas your solution shows you’re trying to parse it inside some environment which you didn’t specify in your question.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest