How to use function transform("JSONPATH","$.append()",)

When using functions in transform("JSONPATH") like $.length() they work just fine. However, I am struggeling with $.append()…
The following code is supposed to add a new “record” to the existing JSON string stored in item vAlarme

var jsonAlarm = "\"" + + "\": { \"device\": \"" + alertDevice + "\", \"location\": \"" + alertLocation + "\", \"state\": \"" + newState + "\", \"date\": \"" + now().format(DateTimeFormatter.ofPattern("dd.MM., HH:mm")) + "\"}"
var tmp = transform("JSONPATH", "$.append(jsonAlarm)", vAlarme.state.toString())

The output of tmp however, is not the string to be added but the existing json string to which I want to add a new record.
On the other side, the syntax how I have been using it, makes logically sense.
Before testing around, does anybody know how to use .append()?

most probably the JSON string causes some problems as it is always “converted” when writing to the item. All “:” are being replaced by “=” and all double quotes are deleted.

This is the JSON string stored in vAlarme item:

{"TestItem2": { "device": "ShellyWZSofalampe", "location": "Wohnzimmer", "state": "EASY", "date": "25.02., 14:28"}}

JSON string to be added:
"TestItem": { "device": "ShellyWZSofalampe", "location": "Wohnzimmer", "state": "EASY", "date": "25.02., 14:31"}

The JSONPATH transform dies not orbit a full implementation of JSONPATH. I don’t think it supports adding to the JSON.

The transformations are designed to extract data, not adding data.

You will probably need to import the JSON classes into your rule and use those to parse and manipulate the JSON.

1 Like