How to properly parse returned JSON array

Experimenting in rules can be useful when trying to get a transformation working.


val rawjson = '{"Time":"2020-05-24T18:28:31","ENERGY":{"TotalStartTime":"2020-05-24T18:28:28","Total":49.044,"Yesterday":1.943,"Today":9.842,"Period":1,"Power":[70,55,165],"ApparentPower":[146,354,196],"ReactivePower":[128,350,106],"Factor":[0.48,0.15,0.84],"Frequency":[50,50,50],"Voltage":[239,239,238],"Current":[0.611,1.485,0.824]},"DS18B20":{"Id":"0015232FE7EE","Temperature":24.5},"TempUnit":"C"}'

var results = transform("JSONPATH", "$.ENERGY.Total", rawjson)
logInfo("test", "simple " + results)

results = transform("JSONPATH", "$.ENERGY.Voltage", rawjson)
logInfo("test", "array " + results)

results =  transform("JSONPATH", "$.ENERGY.Voltage.[0]", rawjson)
logInfo("test", "element " + results)

output

2020-05-26 13:36:27.102 [INFO ] [.eclipse.smarthome.model.script.test] - simple 49.044
2020-05-26 13:36:27.106 [INFO ] [.eclipse.smarthome.model.script.test] - array [239, 239, 238]
2020-05-26 13:36:27.111 [INFO ] [.eclipse.smarthome.model.script.test] - element 239
1 Like