Jsonpath parsing of NOAA Aurora fails with IllegalArgumentException

Hello community colleagues :slight_smile:

hopefully you can help me with an issue that I have regarding jsonpath items.

With OH 2.5 I want to parse the probability of Aurora events for a certain location into items by using the JSON provides by NOAA: https://services.swpc.noaa.gov/json/ovation_aurora_latest.json.

I have the following entries in the http.cfg:

auroraNoaaGov.url=https://services.swpc.noaa.gov/json/ovation_aurora_latest.json
auroraNoaaGov.updateInterval=300000 

My items file has those entires:

Number		AuroraPropabilityHome		"Aurora Home [%.0f%%]"		<home>				{http="<[auroraNoaaGov:300000:JSONPATH($.coordinates[?(@[0] == 51 && @[1] == 6)].2)]" } 
Number		AuroraPropabilityOther		"Aurora Other [%.0f%%]"		<other>				{http="<[auroraNoaaGov:300000:JSONPATH($.coordinates[?(@[0] == 57 && @[1] == 15)].2)]" } 

I successfully tested the JSONPATH at Online JSON Query Tester (jsonquerytool.com) using the Goessner 0.8.0 transform and it succeeded:

My logfiles have the following error:

2021-11-06 11:15:10.497 [ERROR] [b.core.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service

java.lang.IllegalArgumentException: given transformation function 'JSONPATH($.coordinates[?(@[0' does not follow the expected pattern '<function>(<pattern>)'

	at org.openhab.binding.http.internal.HttpBinding.splitTransformationConfig(HttpBinding.java:261) ~[?:?]

	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:211) ~[?:?]

	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:146) ~[bundleFile:?]

	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169) [bundleFile:?]

Where is my fault - I don’t see it.
Could you please help?

I’d say it has fallen down at the nested closing square bracket ]
Don’t think I’ve ever seen nested [ ] used with OH JSONPATH transformation.

Thanks for this hint - but evaluation either with the one mentioned above or with jsonpath online evaluator failed.
Is there a better way to search for values in multidimensional indexed-only arrays?

Use a technique for playing in a rule to try your JSONPATH params.

val rawjson = '{"Observation Time": "2021-11-06T11:06:00Z", "Forecast Time": "2021-11-06T11:52:00Z", "Data Format": "[Longitude, Latitude, Aurora]", "coordinates": [[0, -90, 5], [0, -89, 0]] }'
var results = transform("JSONPATH", "$.coordinates.[1].[1]", rawjson)
logInfo("test", " simple path to one value: " + results)
results = transform("JSONPATH", "$.coordinates.[?(@.[1]=='-89')].[1]", rawjson)
logInfo("test", " selective path to one value: " + results)

Partial example of searching

Thank you for this tipp.

I created the following rule:

rule "Aurora Test"
when 
	Time cron "0 0/2 * * * ? *"
then
	val rawjson = '{"Observation Time": "2021-11-06T11:06:00Z", "Forecast Time": "2021-11-06T11:52:00Z", "Data Format": "[Longitude, Latitude, Aurora]", "coordinates": [[57, 15, 80], [51, 6, 70], [0, -90, 5], [0, -89, 0]] }'
	var results = transform("JSONPATH", "$.coordinates.[1].[2]", rawjson)
	logInfo("[HDMX] Aurora Test", "Simple path to one value: " + results)
	
	results = transform("JSONPATH", "$.coordinates.[?(@.[1]=='-89')].[2]", rawjson)
	logInfo("[HDMX] Aurora Test", "Selective path to one value: " + results)
	
	results = transform("JSONPATH", "$.coordinates.[?(@[0] == 57 && @[1] == 15)].[2]", rawjson)
	logInfo("[HDMX] Aurora Test", "Desired path to one value: " + results)
	
	results = transform("JSONPATH", "$['coordinates'].[?(@[0] == 57 && @[1] == 15)].[2]", rawjson)
	logInfo("[HDMX] Aurora Test", "Another try: " + results)
end

The output is as follows:

2021-11-06 13:56:00.002 [INFO ] [home.model.script.[HDMX] Aurora Test] - Simple path to one value: 70
2021-11-06 13:56:00.003 [INFO ] [home.model.script.[HDMX] Aurora Test] - Selective path to one value: 0
2021-11-06 13:56:00.003 [INFO ] [home.model.script.[HDMX] Aurora Test] - Desired path to one value: 80
2021-11-06 13:56:00.004 [INFO ] [home.model.script.[HDMX] Aurora Test] - Another try: 80

Assuming that it works, I tried both variants “$.coordinates.[?(@[0] == 57 && @[1] == 15)].[2]” and “$[‘coordinates’].[?(@[0] == 57 && @[1] == 15)].[2]” with the item:

Number		AuroraPropabilityOther		"Aurora Other [%.0f%%]"	{http="<[auroraNoaaGov:10000:JSONPATH($.coordinates.[?(@[0] == 57 && @[1] == 15)].[2])]" }

But it this fails with “java.lang.IllegalArgumentException: given transformation function ‘JSONPATH($[‘coordinates’].[?(@[0’ does not follow the expected pattern ‘()’”

If I use simple path like the following, it works fine:

Number		AuroraPropabilityHome		"Aurora Home [%.0f%%]"	{http="<[auroraNoaaGov:300000:JSONPATH($['coordinates'].[9327].[2])]" }

But as I don’t not if the index will change, I will use a rule for filling the values into the items - not what I prefer, but its ok.

Something funny about the way the Item definition parser is reading your JSONPATH parameters,

it’s going wrong around the closing ]
Try removing all those extraneous spaces.

Perfect, thank you!!!

I removed the spaces and now the JsonPath works fine:

Number	AuroraPropabilityOther	"Aurora Other [%.0f%%]"	{http="<[auroraNoaaGov:300000:JSONPATH($.coordinates[?(@[0]==57 && @[1]==15)].[2])]" }

Thank you for this useful advise :slight_smile:

2 Likes