JSONPATH numeric filtering not working

Hi,

I’m trying to use JSONPATH numeric filtering on a HTTP bindings channel. While I can do string filtering, numeric filtering is not working.

JSONPATH:$..[?(@.price=='15')] works and returns the JSON element of interest
JSONPATH:$..[?(@.price<16)] does not work and returns NULL

The channel I setup as http:string but tried http:number too, no difference. This is with OH3.3.

Thanks!

Could you provide an example data set ?

… sure, a data set looks like this:

{"elements":[{"begin":"2023-01-22T10:00:00+01:00","end":"2023-01-22T11:00:00+01:00","price":15.128},{"begin":"2023-01-22T11:00:00+01:00","end":"2023-01-22T12:00:00+01:00","price":16.175},{"begin":"2023-01-22T12:00:00+01:00","end":"2023-01-22T13:00:00+01:00","price":16.155},{"begin":"2023-01-22T13:00:00+01:00","end":"2023-01-22T14:00:00+01:00","price":15}],"next_offset":null}

btw, doing filtering like this works: JSONPATH$..price.min() which returns the minimum price of “15”. But I want the whole JSON element for further postprocessing.

Excerpt from JsonPath - Transformation Services | openHAB :

If the JsonPath expression provided results in no matches, the transformation will return the entire original JSON string.

So it shouldn’t return null. Are we sure that HTTP - Bindings | openHAB does use the JSONPATH transformation service provided by openHAB?

… actually, the JSONPATH transformation is working with the numeric filter. I verified by enabling DEBUG on the JSONPATH bundle. The NULL is actually presented on the linked string item. There is a warning in the openhab.log from the JSONPATH bundle:

2023-01-22 14:42:07.641 [DEBUG] [ternal.JSonPathTransformationService] - about to transform '{"elements":[{"begin":"2023-01-22T23:00:00+01:00","end":"2023-01-23T00:00:00+01:00","price":15.356},{"begin":"2023-01-23T00:00:00+01:00","end":"2023-01-23T01:00:00+01:00","price":14.896},{"begin":"2023-01-23T01:00:00+01:00","end":"2023-01-23T02:00:00+01:00","price":14.9}],"next_offset":null}' by the function '$..[?(@.price<15)]'
2023-01-22 14:42:07.642 [DEBUG] [ternal.JSonPathTransformationService] - transformation resulted in '[{"begin":"2023-01-23T00:00:00+01:00","end":"2023-01-23T01:00:00+01:00","price":14.896},{"begin":"2023-01-23T01:00:00+01:00","end":"2023-01-23T02:00:00+01:00","price":14.9}]'
2023-01-22 14:42:07.642 [WARN ] [ternal.JSonPathTransformationService] - JsonPath expressions with more than one result are only supported for Boolean, Number and String data types, please adapt your selector. Result: [{"begin":"2023-01-23T00:00:00+01:00","end":"2023-01-23T01:00:00+01:00","price":14.896},{"begin":"2023-01-23T01:00:00+01:00","end":"2023-01-23T02:00:00+01:00","price":14.9}]

The item I linked to the channel is of type string. So what’s the deal with the “selector”?

Fun fact: when the numeric filter is returning a single JSON element, the linked string item is presenting it just fine. So any hint how to access the filtered multiple JSON elements by a linked item?

Edit: the fine difference of single vs. multiple results is the square brackets. Also the missing quotes. So single results is indeed a string, but multiple results is a JSON still?!

'{begin=2023-01-23T03:00:00+01:00, end=2023-01-23T04:00:00+01:00, price=14.759}'
vs.
[{"begin":"2023-01-23T00:00:00+01:00","end":"2023-01-23T01:00:00+01:00","price":14.896},{"begin":"2023-01-23T01:00:00+01:00","end":"2023-01-23T02:00:00+01:00","price":14.9},{"begin":"2023-01-23T02:00:00+01:00","end":"2023-01-23T03:00:00+01:00","price":14.84},{"begin":"2023-01-23T03:00:00+01:00","end":"2023-01-23T04:00:00+01:00","price":14.759},{"begin":"2023-01-23T04:00:00+01:00","end":"2023-01-23T05:00:00+01:00","price":14.988}]

Thanks

… ok, final assessment - its not working with multiple results. The JSONPATH transformation service is not outputting multi-element results.
I tried to be clever and chain a second JSONPATH transformation, yet that failed due to no input:

JSONPATH:$..[?(@.price<15)]∩JSONPATH:$..begin

generates:

2023-01-22 15:45:21.129 [DEBUG] [ternal.JSonPathTransformationService] - transformation resulted in '[{"begin":"2023-01-23T00:00:00+01:00","end":"2023-01-23T01:00:00+01:00","price":14.896},{"begin":"2023-01-23T01:00:00+01:00","end":"2023-01-23T02:00:00+01:00","price":14.9},{"begin":"2023-01-23T02:00:00+01:00","end":"2023-01-23T03:00:00+01:00","price":14.84},{"begin":"2023-01-23T03:00:00+01:00","end":"2023-01-23T04:00:00+01:00","price":14.759},{"begin":"2023-01-23T04:00:00+01:00","end":"2023-01-23T05:00:00+01:00","price":14.988}]'
2023-01-22 15:45:21.129 [DEBUG] [ternal.JSonPathTransformationService] - about to transform 'NULL' by the function '$..begin'

Shall I file an issue on Github?

Documentation and behaviour don’t match. IMHO you have stumbled upon a bug (or documentation error …), see JSONPATH is no JSONPATH anymore. · Issue #5132 · eclipse-archived/smarthome · GitHub .

Edit:
Workaround would be to use jq (or a similar tool) and Exec binding, see JSON transform problem in OH 2.3 - #3 by job .

Edit #2:

1 Like

Hi BK,

thanks for the pointers. That is indeed the same issue - no support for multiple results. Single results have been addressed by turning them into a String. Why does this not work for multiple results? Do we need a JSON object?

I checked out the jq tool. Luckily the OH Docker container comes with this tool preinstalled. With that I can process the JSON and filter it down to the info I want - on the command line. I guess I must switch from http binding to rule based sendHttpGetRequest to fit my needs?!