Hello,
I am currently having a String
item which contains an array. When this item is changed, it triggers a rule and the rule sends a HTTP Put Request
with the first element in the array as body.
I do want to remove the first element in that array when I get the response, so a queue will be simulated by triggering the rule again.
Note:
This is for a nodejs local API which gets an item name and a tag and adds the tag to .items file. Any other advice on how to handle this would be great!
This is the Item
[
{"name": "SomeItem_Status", "tag": "SomeTag"},
{"name": "SomeItem_Status", "tag": "SomeTag"},
{"name": "SomeItem_Status", "tag": "SomeTag"}
]
This is the rule
rule "Update item tags in webAPI"
when
Item ItemsToModify changed
then
var itemName = transform("JSONPATH", "$.[0].name", ItemsToModify.state.toString)
var itemTag = transform("JSONPATH", "$.[0].tag", ItemsToModify.state.toString)
var requestContent = '{"name": "' + itemName + '", "tag": "' + itemTag + '"}'
sendHttpPutRequest("http://localhost:61000/addTag", "application/json", requestContent)
/* now we should delete the first element of the array */
ItemsToModify.sendCommand(transform("JSONPATH", "$.[1:]", ItemsToModify.state.toString))
end
Sending the request and editing the item works, but I can not find a way to remove the element. The JSONPATH
returns a warning and my object is null
after that.
The warning:
JsonPath expressions with more than one result are only supported for Boolean, Number and String data types, please adapt your selector.
Result: [{"name":"SomeItem_Status","tag":"SomeItem_Tag"},{"name":"SomeItem_Status","tag":"SomeItem_Tag"}]