How to parse JSON in LUA Topic is solved

Moderator: leecollings

Post Reply
thetrueavatar
Posts: 3
Joined: Friday 01 December 2017 21:22
Target OS: -
Domoticz version:
Contact:

How to parse JSON in LUA

Post by thetrueavatar »

Hello,
I was wondering how do you parse json in LUA to extract information ? I have tried domoticz_applyJsonPath without success.
I have got a json response such as below and would like to get the value field data based on provided dataPointName in metadata.
I was trying to use kind of regex with the find method on String but was wondering if there was an easier way to do this in lua ?:

Code: Select all

{
  "data": [    
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein",
          "2": "Aus",
          "3": "Ein"
        },
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "245",
        "datapointType": "ENUM",
        "datapointName": "zustand_interne_pumpe_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein"
        },
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "600",
        "datapointType": "ENUM",
        "datapointName": "zustand_brenner_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "1": "1 A1",
          "2": "2 A1  + WW",
          "3": "3 M2",
          "4": "4 M2 +  WW",
          "5": "5 A1  + M2",
          "6": "6 A1  + M2 + WW"
        },
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "791",
        "datapointType": "ENUM",
        "datapointName": "konf_heizungsschema_r",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "2",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "inaktiv",
          "1": "aktiv"
        },
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "865",
        "datapointType": "ENUM",
        "datapointName": "zustand_wartung_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "0 ohne",
          "1": "1 Vitosolic 100",
          "2": "2 Vitosolic 200"
        },
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "940",
        "datapointType": "ENUM",
        "datapointName": "konf_solarregler_r",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "OK",
          "1": "Kurzschluss",
          "2": "Unterbrechung",
          "3": "unbekannt",
          "4": "unbekannt",
          "5": "unbekannt",
          "6": "Nicht vorhanden"
        },
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "10796",
        "datapointType": "ENUM",
        "datapointName": "info_status_rts_1_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "3",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "OK",
          "1": "Kurzschluss",
          "2": "Unterbrechung",
          "3": "unbekannt",
          "4": "unbekannt",
          "5": "unbekannt",
          "6": "Nicht vorhanden"
        },
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "10797",
        "datapointType": "ENUM",
        "datapointName": "info_status_rts_2_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "3",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "76",
        "datapointType": "ENUM",
        "datapointName": "zustand_partybetrieb_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "88",
        "datapointType": "ENUM",
        "datapointName": "zustand_sparbetrieb_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Abschalt",
          "1": "Nur WW",
          "2": "Heizen + WW",
          "3": "Dauernd Reduziert",
          "4": "Dauernd Normal"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "92",
        "datapointType": "ENUM",
        "datapointName": "konf_betriebsart_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "2",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "270",
        "datapointType": "ENUM",
        "datapointName": "externe_umschaltung_ba_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "112",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Abschaltbetrieb",
          "1": "Reduzierter Betrieb",
          "2": "Normalbetrieb",
          "3": "Dauernd Normalbetrieb"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "708",
        "datapointType": "ENUM",
        "datapointName": "aktuelle_betriebsart_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "2",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "inaktiv",
          "1": "aktiv"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "714",
        "datapointType": "ENUM",
        "datapointName": "zustand_ferienprogramm_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "inaktiv",
          "1": "aktiv"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "717",
        "datapointType": "ENUM",
        "datapointName": "zustand_frostgefahr_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "729",
        "datapointType": "ENUM",
        "datapointName": "zustand_heizkreispumpe_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Nicht vorhanden",
          "1": "Direkter Kreis",
          "2": "Mischerkreis"
        },
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "7987",
        "datapointType": "ENUM",
        "datapointName": "info_kennung_hk1_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein"
        },
        "datapointGroup": "WW",
        "heatingCircuitId": "18954",
        "datapointId": "5280",
        "datapointType": "ENUM",
        "datapointName": "zustand_speicherladepumpe_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Ladung inaktiv",
          "1": "in Ladung",
          "2": "im Nachlauf"
        },
        "datapointGroup": "WW",
        "heatingCircuitId": "18954",
        "datapointId": "7179",
        "datapointType": "ENUM",
        "datapointName": "info_ww_charging_active_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "values": {
          "0": "Aus",
          "1": "Ein"
        },
        "datapointGroup": "WW",
        "heatingCircuitId": "18954",
        "datapointId": "7181",
        "datapointType": "ENUM",
        "datapointName": "zustand_zirkulationspumpe_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 1,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "0.00",
        "defaultValue": "",
        "unitDesignation": "ecnUnit.Stunden",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "104",
        "datapointType": "Double",
        "datapointName": "anzahl_brennerstunden_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "5994",
      "dataType": 3,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "1193045.00",
        "defaultValue": "",
        "unitDesignation": "",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "111",
        "datapointType": "Integer",
        "datapointName": "anzahl_brennerstart_r",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "33861",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "0.00",
        "defaultValue": "",
        "unitDesignation": "",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "897",
        "datapointType": "Integer",
        "datapointName": "info_fehlerbyte_gfa_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "0",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "100.00",
        "defaultValue": "",
        "unitDesignation": "ecnUnit.Prozent",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "4165",
        "datapointType": "Double",
        "datapointName": "info_brenner_modulation_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "25",
      "dataType": 3,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "500.00",
        "defaultValue": "",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "5372",
        "datapointType": "Double",
        "datapointName": "temp_agt_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "38",
      "dataType": 3,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "-60.00",
        "maximalValue": "60.00",
        "defaultValue": "",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "5373",
        "datapointType": "Double",
        "datapointName": "temp_ats_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1.8",
      "dataType": 3,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "127.00",
        "defaultValue": "",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "5374",
        "datapointType": "Double",
        "datapointName": "temp_kts_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "41",
      "dataType": 3,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "0.00",
        "defaultValue": "",
        "unitDesignation": "",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "5385",
        "datapointType": "Date",
        "datapointName": "konf_uhrzeit_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "12/3/2017 12:35:06 PM",
      "dataType": 4,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "0.00",
        "defaultValue": "",
        "unitDesignation": "",
        "datapointGroup": "DEFAULT-GROUP",
        "heatingCircuitId": "18947",
        "datapointId": "7184",
        "datapointType": "String",
        "datapointName": "aktuelle_fehler_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "00000000000000000000",
      "dataType": 6,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "10.00",
        "maximalValue": "95.00",
        "defaultValue": "50",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "51",
        "datapointType": "Integer",
        "datapointName": "konf_ww_solltemp_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "50",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "4.00",
        "maximalValue": "37.00",
        "defaultValue": "20",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "79",
        "datapointType": "Integer",
        "datapointName": "konf_partysolltemp_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "19",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "3.00",
        "maximalValue": "37.00",
        "defaultValue": "20",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "82",
        "datapointType": "Integer",
        "datapointName": "konf_raumsolltemp_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "20",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "3.00",
        "maximalValue": "37.00",
        "defaultValue": "3",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "85",
        "datapointType": "Integer",
        "datapointName": "konf_raumsolltemp_reduziert_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "15",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "0.00",
        "defaultValue": "1970-01-01 00:00:00",
        "unitDesignation": "",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "306",
        "datapointType": "Date",
        "datapointName": "konf_ferien_start_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1/1/1970 12:00:00 AM",
      "dataType": 4,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "0.00",
        "defaultValue": "1970-01-01 00:00:00",
        "unitDesignation": "",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "309",
        "datapointType": "Date",
        "datapointName": "konf_ferien_ende_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1/1/1970 12:00:00 AM",
      "dataType": 4,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.20",
        "maximalValue": "3.50",
        "defaultValue": "1,4",
        "unitDesignation": "",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "2869",
        "datapointType": "Double",
        "datapointName": "konf_neigung_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "1",
      "dataType": 3,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "-13.00",
        "maximalValue": "40.00",
        "defaultValue": "0",
        "unitDesignation": "ecnUnit.K",
        "datapointGroup": "HC1",
        "heatingCircuitId": "18949",
        "datapointId": "2875",
        "datapointType": "Double",
        "datapointName": "konf_niveau_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "-5",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "10.00",
        "maximalValue": "95.00",
        "defaultValue": "50",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "WW",
        "heatingCircuitId": "18954",
        "datapointId": "51",
        "datapointType": "Integer",
        "datapointName": "konf_ww_solltemp_rw",
        "isWritable": true,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "50",
      "dataType": 2,
      "datapointType": 0,
      "datapointStatus": 0
    },
    {
      "metadata": {
        "minimalValue": "0.00",
        "maximalValue": "150.00",
        "defaultValue": "",
        "unitDesignation": "ecnUnit.Grad C",
        "datapointGroup": "WW",
        "heatingCircuitId": "18954",
        "datapointId": "5381",
        "datapointType": "Double",
        "datapointName": "temp_ww_r",
        "isWritable": false,
        "isReadable": true,
        "datapointTypeValue": 0
      },
      "value": "43.1",
      "dataType": 3,
      "datapointType": 0,
      "datapointStatus": 0
    }
  ]
}
thetrueavatar
Posts: 3
Joined: Friday 01 December 2017 21:22
Target OS: -
Domoticz version:
Contact:

Re: How to parse JSON in LUA

Post by thetrueavatar »

Ok found my own solution. I have used the json:decode to get a LUA table and iterate over it.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: How to parse JSON in LUA

Post by rrozema »

Can you maybe post a bit of the code you found to work as an example for me and others?
rjblake
Posts: 142
Joined: Friday 21 October 2016 9:25
Target OS: NAS (Synology & others)
Domoticz version:
Location: Netherlands
Contact:

Re: How to parse JSON in LUA

Post by rjblake »

rrozema wrote: Tuesday 05 December 2017 7:57 Can you maybe post a bit of the code you found to work as an example for me and others?
Check out the code here for JSON use in LUA http://domoticz.com/forum/viewtopic.php?f=61&t=13318
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest