I just discovered that the JSONPATH transformation doesn’t handle null-values well after upgrading to 2.2 stable. I have a rule that checks for a software update for my heatpump. The rule calls a script which returns a json-string. If no update is available it looks like
{"current":{"name":"8461R2","version":8461,"release":2},"upgrade":null}
But when I try to extract the “upgrade” value using
var update = transform("JSONPATH", "$.upgrade", output)
it returns the whole json string if the value is null.
I did some additional tests to check:
rule "test"
when
Item Test changed
then
var json1 = "{\"current\":{\"name\":\"8461R2\",\"version\":8461,\"release\":2},\"upgrade\":\"null\"}"
var json2 = "{\"current\":{\"name\":\"8461R2\",\"version\":8461,\"release\":2},\"upgrade\":null}"
logInfo("Test", transform("JSONPATH", "$.upgrade", json1))
logInfo("Test", transform("JSONPATH", "$.upgrade", json2))
end
which outputs:
2018-01-03 15:07:18.458 [INFO ] [.eclipse.smarthome.model.script.Test] - null
2018-01-03 15:07:18.465 [INFO ] [.eclipse.smarthome.model.script.Test] - {"current":{"name":"8461R2","version":8461,"release":2},"upgrade":null}
If it’s a string “null” everything works, but a null-value doesn’t.
I didn’t have this issue in 2.1.
Edit: After checking the source code for the transformationservice this is apparently by design:
return (transformationResult != null) ? transformationResult.toString() : source;
and was changed in https://github.com/eclipse/smarthome/commit/f1c63ba7177bf007b457f60676c8701e87c74343
So how should this be handled?