Help needed for JSONPATH transformation

Hi all,

can someone help mie with the transformation of my JSON channel input:

{"dxsEntries":[{"dxsId":67109120,"value":5860.546875},{"dxsId":16780032,"value":3}],"session":{"sessionId":0,"roleId":0},"status":{"code":0}}

I need the values in separate channels, therfore I want to use the transformation feature in the channel configuration.

Go to Best JSON Viewer Online and paste your JSON there

Type number : a0   "a0"              [  transformationPattern="JSONPATH:$.dxsEntries[0].dxsId"]
Type number : a1   "a1"              [  transformationPattern="JSONPATH:$.session.sessionId"]
Type number : a2   "a2"              [  transformationPattern="JSONPATH:$.session.value"]
Type number : a3   "a3"              [  transformationPattern="JSONPATH:$.status.code"]
1 Like

You could us a bit more. Knowing the binding involved helps to know what options are available to you.
Does the JSON always come in this order, or with the same number of elements? Do you know those dxsId codes in advance, are you looking to extract the associated values to your channels?

There are quite a few posts about getting data from JSON with particular RFID codes, which might help.

Good questions, sorry that I forgot to mention that:
I would not rely on the order, but I do know the dxsIds, and I’ interested onyl in the value of the different IDs

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("test", " first array element value " + results)

// next, instead of by position like [0] or [1]
// we can select array elements by a ? query match to ID field
results = transform("JSONPATH", "$.dxsEntries.[?(@.dxsId==16780032)].value", rawjson)
logInfo("test", " path matching " + results)
results = transform("JSONPATH", "$.dxsEntries.[?(@.dxsId==67109120)].value", rawjson)
logInfo("test", " path matching " + results)
results = transform("JSONPATH", "$.dxsEntries.[?(@.dxsId==99)].value", rawjson)
logInfo("test", " path matching " + results)

first array element ID 67109120
first array element value 5860.546875
path matching 3
path matching 5860.546875
path matching NULL

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