LUA: Parse json with a variable

Post Reply
chatainsim
Posts: 87
Joined: Wednesday 25 March 2015 11:46
Target OS: Linux
Domoticz version: v3.9439
Location: France
Contact:

LUA: Parse json with a variable

Post by chatainsim » Friday 12 January 2018 2:01

Hello,

I'm trying to parse a json file with JSON.lua module.
I get a ID from json and use this ID to get another value in the json data, like this:

Code: Select all

    local file = assert(io.open(config, "r"))
    local json_text = file:read("*all")
    file:close()
    local Conf = json:decode(json_text)
    Station = Conf.station
for k,v in pairs(Station) do
        local IDStation = v
        local IDXHeight = Conf.idx.height.v
        local IDXSpeed = Conf.idx.speed.v
        local NotifHeightV = Conf.notifications.height.v.value
        [...]
But the v variable is not replace in Conf.idx.height.v.
How can I force lua to remplace v with the value of the v variable ?

To get this for example:

Code: Select all

local IDXHeight = Conf.idx.height.L800001020
local NotifHeightV = Conf.notifications.height.L800001020.value
my json config file look like this:

Code: Select all

{
  "station": [
    "L800001020",
    "W141001001"
  ],
  "idx": {
    "height": {
      "L800001020": "218",
      "W141001001": "215"
    },
    "speed": {
      "L800001020": "217",
      "W141001001": "216"
    }
  },
  "notifications": {
    "height": {
      "W141001001": {
        "test": "ge",
        "value": "2",
        "active": "true"
      },
      "L800001020": {
        "test": "gt",
        "value": "1",
        "active": "true"
      }
    },
    "speed": {
      "W141001001": {
        "test": "ge",
        "value": "100",
        "active": "true"
      },
      "L800001020": {
        "test": "ge",
        "value": "140",
        "active": "true"
      }
    }
  }
}
Thanks

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests