JSON into JSON

Hi, i want extract the Temp of an HTTP THING, this is the JSON code

{
"metadata": [
  [
  "device_id",
  "datatime_utc_last_data",
  "api_data_return"
],
],
"value": [
  [
  "BB-01-TERM2",
  "2022-10-03 12:12:24.712",
  "{"setGuestFan":"0","setGuestTemp":26.0,"currentTemp":27.0,"currentFan":0}"
],
],
}

if i set the JSONPath Expression

$.value[0][2]

the result is

{"setGuestFan":"0","setGuestTemp":26.0,"currentTemp":27.5,"currentFan":0}

i want currentTemp

this is a JSON into a JSON

It’s possible to create a JS, to convert it?

Can you help me

OK, you change this while I was responding. But in both cases, neither the original version nor this new pretty printed version is valid JSON.

But assuming that it will work in OH with the escaped double quotes, your JSONPATH has given you exactly what you’ve asked for, the element “value”'s first element of the outer array and the third element of the inner array. The third element is, as you can see from the pretty print:

"{\"setGuestFan\":\"0\",\"setGuestTemp\":26.0,\"currentTemp\":27.0,\"currentFan\":0}"

The escaped double quotes (i.e. \") make that not valid JSON. In fact, the fact that the whole thing is in double quotes makes it not valid JSON. It’s a String of a JSON. I double JSONPATH will be able to handle that. But if we assume it could work, you need to tell it which element you want.

$.value[0][3].currentTemp

That almost certainly won’t work though because it’s not valid JSON so you’ll have to extract the value some other way, such as in a rule or using the REGEX transformation.

Try

$.value[0][2]∩$.currentTemp

Note: is not n

I created a JS

(function(val){
    var n = JSON.parse(val).value[0][2];
    return JSON.parse(n).currentTemp;
})(input);

Te result is 27.5

1 Like