rossko57
(Rossko57)
November 18, 2021, 8:50pm
2
Yes.
If you want to use the JSONPATH transformation, you’ll probably do one element at a time, because the transformation only returns one string.
Examples
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 result…
I find it’s worth working out complex JSONPATH transforms in a rule.
val rawjson = '{"dxsEntries":[{"dxsId":67109120,"value":5860.546875},{"dxsId":16780032,"value":3}],"session":{"sessionId":0,"roleId":0},"status":{"code":0}}'
// first we'll make sure we can extract array elements by position
var results = transform("JSONPATH", "$.dxsEntries.[0].dxsId", rawjson)
logInfo("test", " first array element ID " + results)
results = transform("JSONPATH", "$.dxsEntries.[0].value", rawjson)
logInfo("tes…
You can get it to return a single string that looks like an array “[blah, bleh]” which you can parse further in your rule (e.g. split on commas) but likely easier to get one at a time.
Complex example doing that
There was recent interest in this, but in a forum hiccup the recent posts got lost.
It did remind me I’d started to play with this out of interest, but never finished. So now I did.
Using your JSON -
import java.util.HashMap
import java.util.ArrayList
import java.util.Collections
val rawjson = '[
{"periodType" : "ACTUAL","semiScheduledGeneration" : "312.66","operationalDemand" : "4728.94","rooftopSolar" : "0","createdAt" : "2020-10-18T21:05:41","wholesaleKWHPrice" : "4.7509","region" : "VI…