How to find jsonpath from string?

hi i have string form solarpanel
{“deviceSn”: “SA3ES233P5Q037”, “deviceId”: 228706985, “deviceType”: “INVERTER”, “deviceState”: 1, “collectionTime”: 1697809508, “dataList”: {“SN1”: {“value”: “SA3ES233P5Q037”, “unit”: null, “name”: “SN”}, “SS_CY1”: {“value”: “22”, “unit”: null, “name”: “Production Compliance Country”}, “MODEi1”: {“value”: “0”, “unit”: null, “name”: “Input Mode”}, “SYSTIM1”: {“value”: “23-10-20 15:44:35”, “unit”: null, “name”: “System Time”}, “HWv1”: {“value”: “V200”, “unit”: null, “name”: “Hardware Version”}, “SWmai_v1”: {“value”: “G410”, “unit”: null, “name”: “Software Master Version”}, “PTCv1”: {“value”: “110”, “unit”: null, “name”: “Protocol Version”}, “DSPv1”: {“value”: “V410”, “unit”: null, “name”: “DSP Version”}, “DSPv2”: {“value”: “V410”, “unit”: null, “name”: “Vice DSP Version”}, “FUSE_V”: {“value”: “”, “unit”: null, “name”: “FUSE Version”}, “DV1”: {“value”: “313.50”, “unit”: “V”, “name”: “DC Voltage PV1”}, “DV2”: {“value”: “0.00”, “unit”: “V”, “name”: “DC Voltage PV2”}, “DC1”: {“value”: “0.00”, “unit”: “A”, “name”: “DC Current PV1”}, “DC2”: {“value”: “0.00”, “unit”: “A”, “name”: “DC Current PV2”}, “DP1”: {“value”: “0”, “unit”: “W”, “name”: “DC Power PV1”}, “DP2”: {“value”: “0”, “unit”: “W”, “name”: “DC Power PV2”}, “AV1”: {“value”: “237.60”, “unit”: “V”, “name”: “AC Voltage R/U/A”}, “AV2”: {“value”: “0.00”, “unit”: “V”, “name”: “AC Voltage S/V/B”}, “AV3”: {“value”: “0.00”, “unit”: “V”, “name”: “AC Voltage T/W/C”}, “AC1”: {“value”: “0.30”, “unit”: “A”, “name”: “AC Current R/U/A”}, “AC2”: {“value”: “0.00”, “unit”: “A”, “name”: “AC Current S/V/B”}, “AC3”: {“value”: “0.00”, “unit”: “A”, “name”: “AC Current T/W/C”}, “A_Fo1”: {“value”: “49.97”, “unit”: “Hz”, “name”: “AC Output Frequency R”}, “APo_t1”: {“value”: “20”, “unit”: “W”, “name”: “Total AC Output Power (Active)”}, “Et_ge0”: {“value”: “94.00”, “unit”: “kWh”, “name”: “Cumulative Production (Active)”}, “Etdy_ge1”: {“value”: “1.27”, “unit”: “kWh”, “name”: “Daily Production (Active)”}, “t_gc1”: {“value”: “0.00”, “unit”: “kWh”, “name”: “Cumulative Grid Feed-in”}, “Et_pu1”: {“value”: “0.00”, “unit”: “kWh”, “name”: “Cumulative Energy Purchased”}, “Et_use1”: {“value”: “0.00”, “unit”: “kWh”, “name”: “Cumulative Consumption”}, “INV_T0”: {“value”: “49.00”, “unit”: “\u2103”, “name”: “Temperature- Inverter”}, “T_MDU1”: {“value”: “30”, “unit”: “\u2103”, “name”: “Module temperature1”}, “t_w_hou1”: {“value”: “109”, “unit”: “h”, “name”: “Total Running Hour”}, “CD_TIM1”: {“value”: “60”, “unit”: “s”, “name”: “Countdown Time”}, “Bus_V1”: {“value”: “375.90”, “unit”: “V”, “name”: “Bus Voltage”}, “Dcp1”: {“value”: “65534”, “unit”: null, “name”: “DC Component- Phase A”}, “Dcp2”: {“value”: “0”, “unit”: null, “name”: “DC Component- Phase B”}, “Dcp3”: {“value”: “0”, “unit”: null, “name”: “DC Component- Phase C”}, “EAR_N_Ineg1”: {“value”: “0”, “unit”: “K\u03a9”, “name”: “Insulation Impedance- Cathode to ground”}, “CT_P1”: {“value”: “10.00”, “unit”: “W”, “name”: “CT Power”}, “INV_ST1”: {“value”: “In operation”, “unit”: null, “name”: “Inverter status”}, “N_I1”: {“value”: “914”, “unit”: “K\u03a9”, “name”: “PV1 Insulation Impedance”}, “N_I2”: {“value”: “0”, “unit”: “K\u03a9”, “name”: “PV2 Insulation Impedance”}}}

For example im looking total energy i try to use transformationPattern=“JSONPATH:$.Et_ge0” but its not it :frowning:
can someone help me with finding string and value ? or is ther any toole to show complet json path ?

First, pretty print the JSON so you can see the hierarchy. There’s a command line tool you can install on Linux called jq that can do it or any number of online pages that will do it I’m sure (e.g. JSON Formatter & Validator). From there you’ll discover this isn’t valid JSON!

That’s not necessarily a deal killer. JSON is kind of loosey goosey. But it might be a problem. The issue seems to be mismatched double quotes in the date fileds.

Anyway once you can see the hierarchy the JSONPATH becomes more apparent.

{
   "deviceSn":“SA3ES233P5Q037”,
   "deviceId":228706985,
   "deviceType":"INVERTER",
   "deviceState":1,
   "collectionTime":1697809508,
   "dataList":{
      "SN1":{
         "value":“SA3ES233P5Q037”,
         "unit":null,
         "name":"SN"
      },
      "SS_CY1":{
         "value":“22”,
         "unit":null,
         "name":"Production Compliance Country"
      },
      "MODEi1":{
         "value":“0”,
         "unit":null,
         "name":"Input Mode"
      },
      "SYSTIM1":{
         "value":"“23-10-20 15":"44":35”,
         "unit":null,
         "name":"System Time"
      },
      "HWv1":{
         "value":“V200”,
         "unit":null,
         "name":"Hardware Version"
      },
      "SWmai_v1":{
         "value":“G410”,
         "unit":null,
         "name":"Software Master Version"
      },
      "PTCv1":{
         "value":“110”,
         "unit":null,
         "name":"Protocol Version"
      },
      "DSPv1":{
         "value":“V410”,
         "unit":null,
         "name":"DSP Version"
      },
      "DSPv2":{
         "value":“V410”,
         "unit":null,
         "name":"Vice DSP Version"
      },
      "FUSE_V":{
         "value":"",
         "unit":null,
         "name":"FUSE Version"
      },
      "DV1":{
         "value":“313.50”,
         "unit":"V",
         "name":“DC Voltage PV1”
      },
      "DV2":{
         "value":“0.00”,
         "unit":"V",
         "name":“DC Voltage PV2”
      },
      "DC1":{
         "value":“0.00”,
         "unit":"A",
         "name":“DC Current PV1”
      },
      "DC2":{
         "value":“0.00”,
         "unit":"A",
         "name":“DC Current PV2”
      },
      "DP1":{
         "value":“0”,
         "unit":"W",
         "name":“DC Power PV1”
      },
      "DP2":{
         "value":“0”,
         "unit":"W",
         "name":“DC Power PV2”
      },
      "AV1":{
         "value":“237.60”,
         "unit":"V",
         "name":"AC Voltage R/U/A"
      },
      "AV2":{
         "value":“0.00”,
         "unit":"V",
         "name":"AC Voltage S/V/B"
      },
      "AV3":{
         "value":“0.00”,
         "unit":"V",
         "name":"AC Voltage T/W/C"
      },
      "AC1":{
         "value":“0.30”,
         "unit":"A",
         "name":"AC Current R/U/A"
      },
      "AC2":{
         "value":“0.00”,
         "unit":"A",
         "name":"AC Current S/V/B"
      },
      "AC3":{
         "value":“0.00”,
         "unit":"A",
         "name":"AC Current T/W/C"
      },
      "A_Fo1":{
         "value":“49.97”,
         "unit":"Hz",
         "name":"AC Output Frequency R"
      },
      "APo_t1":{
         "value":“20”,
         "unit":"W",
         "name":"Total AC Output Power (Active)"
      },
      "Et_ge0":{
         "value":“94.00”,
         "unit":"kWh",
         "name":"Cumulative Production (Active)"
      },
      "Etdy_ge1":{
         "value":“1.27”,
         "unit":"kWh",
         "name":"Daily Production (Active)"
      },
      "t_gc1":{
         "value":“0.00”,
         "unit":"kWh",
         "name":"Cumulative Grid Feed-in"
      },
      "Et_pu1":{
         "value":“0.00”,
         "unit":"kWh",
         "name":"Cumulative Energy Purchased"
      },
      "Et_use1":{
         "value":“0.00”,
         "unit":"kWh",
         "name":"Cumulative Consumption"
      },
      "INV_T0":{
         "value":“49.00”,
         "unit":“\u2103”,
         "name":"Temperature- Inverter"
      },
      "T_MDU1":{
         "value":“30”,
         "unit":“\u2103”,
         "name":“Module temperature1”
      },
      "t_w_hou1":{
         "value":“109”,
         "unit":"h",
         "name":"Total Running Hour"
      },
      "CD_TIM1":{
         "value":“60”,
         "unit":"s",
         "name":"Countdown Time"
      },
      "Bus_V1":{
         "value":“375.90”,
         "unit":"V",
         "name":"Bus Voltage"
      },
      "Dcp1":{
         "value":“65534”,
         "unit":null,
         "name":"DC Component- Phase A"
      },
      "Dcp2":{
         "value":“0”,
         "unit":null,
         "name":"DC Component- Phase B"
      },
      "Dcp3":{
         "value":“0”,
         "unit":null,
         "name":"DC Component- Phase C"
      },
      "EAR_N_Ineg1":{
         "value":“0”,
         "unit":“K\u03a9”,
         "name":"Insulation Impedance- Cathode to ground"
      },
      "CT_P1":{
         "value":“10.00”,
         "unit":"W",
         "name":"CT Power"
      },
      "INV_ST1":{
         "value":"In operation",
         "unit":null,
         "name":"Inverter status"
      },
      "N_I1":{
         "value":“914”,
         "unit":“K\u03a9”,
         "name":“PV1 Insulation Impedance”
      },
      "N_I2":{
         "value":“0”,
         "unit":“K\u03a9”,
         "name":“PV2 Insulation Impedance”
      }
   }
}

Now you can see that the root $ has a bunch of properties under it including datalist. Under datalist you have a bunch of entries including ET_ge0. And you see that ET_ge0 has a bunch of properties including name, unit, and value.

So, to get the value the JSONPATH would be something like $.datalist.ET_ge0.value. To get the name the JSONPATH would be something like $.datalist.ET_ge0.name.

A JSONPATH tester can be helpful. https://jsonpath.com/

https://www.baeldung.com/guide-to-jayway-jsonpath is one of the many JSONPATH tutorials on the web. But note that OH has a limitation that it cannot return an array nor handle processing arrays very well. In cases where you have a JSON with a variable entries in an array you might need to use a JS Transform. Similarly, if you need more than one value from the JSON to populate a single Item (e.g. you want the value and the unit to populate a Number:Energy Item) you may need to use a JS transform also.

(function(i) {
  const parsed = JSON.parse(i);
  return parsed.datalist.ET_ge0.value + ' ' + parsed.datalist.ET_ge0.unit;
})(input)
1 Like

thx it now i got it :smiley: just need to conbert string to number so i have to search for some rule or regex

If this is a number Channel type and it’s linked to a Number or Number:Energy type Item, the String will be converted to a number automatically.

1 Like

added
Type number : pro_total [ stateTopic=“solarmanpv/inverter_sa3es233p5q037”,transformationPattern=“JSONPATH:$.datalist.Et_ge0.value”]
and iteam number and its null

First, I don’t really support .things files so if there is a syntax error I won’t be much help.

Has there been a new message published since changing the Thing?

Are you certain the changes to your Thing have been loaded?

Do you see any errors in the log?

Beside the wrong quotes (which may have resulted from not using proper code tags in the first place) I can’t see no obvious invalid JSON.
So the correct Channel should look like this:

Type number : pro_total "Total " [ stateTopic="solarmanpv/inverter_sa3es233p5q037", transformationPattern="JSONPATH:$.datalist.Et_ge0.value", unit="kWh" ]

Please be aware that it’s also possible to set the proper unit.
If the state topic is correct, the channel should work.

Did you install JSONPath?

As you are using text files:
If changing a channel without changing the UID, you’ll have to restart openHAB to force a reload of the channel. So either thange the channel ID or the thing ID, save, then revert the change and save again to ensure openHAB is aware of the changed JSON path.

1 Like

thx, so i used “JSONPATH:$.dataList.Et_ge0.value” and now it works.btw i didnt known dataList and datalist make difference :wink:

Jepp, JSONpath is case sensitive.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.