A rule extracts values using JSONPATH transform every time the input item is changed and set data for each item (here only listed one).
rule "parseRotexInput"
when
Item RotexInput changed
then
var String json = (RotexInput.state as StringType).toString.replace(".0","")
postUpdate(Rotex_mode, Integer::parseInt( transform("JSONPATH", "$.[?(@.name == 'mode')].resp", json).replace("\"","").replace("[","").replace("]","")))
end
My problem: the input value (1.00) is converted to integer 10 instead of 1. If I change parseInt to parseFloat in rule, then I only can display value 1.0 instead of 1. But I only want to see the value 1. What I am doing wrong?
rule "parseRotexInput"
when
Item RotexInput changed
then
var String json = RotexInput.state.toString
Roetx_Mode.postUpdate(Integer::parseInt(transform("JSONPATH", "$.[0].resp", json))
end
Thank you Vincent. This is not working, because of this I had added the replace functions to remove this.
Error during the execution of rule 'parseRotexInput': For input string: "["1.00"]"
I found a working solution. Not simple, but working.
var String json = (RotexInput.state as StringType).toString.replace(".00","").replace(".0","")
Your approach is simpler, so I want to try to use this. jsonpath.com is telling me my JSON string is not valid. Maybe because of the [] instead of {}? Could this cause the error?
I think it’s the ', try replacing it:
It works in jsonpath.com when I replace it.
rule "parseRotexInput"
when
Item RotexInput changed
then
var String json = RotexInput.state.toString.replace("'","\"")
Roetx_Mode.postUpdate(Integer::parseInt(transform("JSONPATH", "$.[0].resp", json))
end