Purpose
- Explain recent history of the functionality, along with faults
- Describe a breaking change that is committed to ESH and is currently in the OH 2.3.0 snapshot
- Describe what I have proposed in the issue that the breaking PR was trying to resolve
- My current workaround
History of functionlity from OH 2.0.0 to OH 2.3.0 snapshot 1194 (January 23) [approximately]
The JSONPATH Transformation Service uses Jayway JSONPATH. When the path is definite, JSONPATH returns a string value, and the JSONPATH transform would return a string. The JSONPATH transform functionality was inline with what is returned by JSONPATH.
When the path is indefinite, JSONPATH returns a List, and the JSONPATH transform would return List.toString. This is not inline with what is returned by JSONPATH and caused a number of issues. For items/labels/bindings, indefinite paths could not be used (ESH issue 4862). For use in a rule, the List.toString that was returned by the transform could be manipulated to return it back to a List. Depending on what was being returned, even double quotes would need to be removed from the result.
val List<String> zwaveThingListFinal = transform("JSONPATH","$..[?(@.UID =~ /zwave:device.*/)].UID",thingResultFinal).replace("[","").replace("]","").replace("\"","").split(",")
Breaking change in ESH
The current OH 2.3.0 snapshots include the ESH PR to resolve the issue linked above. This will allow indefinite paths to be used for items/labels/bindings, if only one element was in the list returned by JSONPATH. If more than one element is returned in the list, the transform will return NULL and log the warning “JsonPath expressions with more than one result are not allowed, please adapt your selector. Result: {}”.
Proposed change
The ESH PR removed more functionality that it created. IMO, the JSONPATH transform should return a list if an indefinite path is used, just as is returned by JSONPATH. This would simplify the use of the transform in rules, but it would break the current functionality for items/labels/bindings unless something could be written to handle this scenario, as the ESH PR has done. A rule could also be used with the JSONPATH transform to populate item values.
Workaround
I’m sure others will run into this, especially if the current change makes it into the production OH build. Until this functionality is implemented, I am using jq where the ESH change had broken several of my rules, but I don’t see why it couldn’t also be used with an EXEC transform for items/labels/bindings too. Hmmm, JQ Transformation Service…
val List<String> thingListFinal = executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s --connect-timeout 10 -m 10 -X GET --header \"Accept: application/json\" \"http://<ohserver>:8080/rest/things\" | /usr/bin/jq '.[].UID | select(contains(\"zwave:device\"))'",5000).split("\n")
At the very least, I hope this helps some people out, but I’m really hoping this post will incite some discussion in the ESH issue too. As of yet, I have not received any response.