Issue with JSONPATH and the sample code

I’ve got OpenHab2 installed as well as the JSONPATH binding. But I’m getting an error when I try and use the sample.

Here is what my rule file looks like

rule “TEST”
when
Time cron “* * * * * ?”
then

var String json = ‘{“store”:{“book”:[{“category”:“reference”,“author”:“Nigel Rees”,“title”: “Sayings of the Century”, “price”: 8.95 } ], “bicycle”: { “color”: “red”, “price”: 19.95} }}’
var test = transform(“JSONPATH”,“$.store.book[0].author”,json)
logInfo(‘test’, json)
end

And here is the error that is being generated:

2016-04-19 12:36:13.007 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule TEST
java.lang.RuntimeException: The name ‘transform(,,)’ cannot be resolved to an item or type.
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:110)[114:org.eclipse.smarthome.model.script:0.8.0.201604051458]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:900)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:863)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:222)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:202)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:825)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:262)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:202)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:445)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:226)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:202)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:188)[128:org.eclipse.xtext.xbase:2.6.2.v201407030533]
at org.eclipse.smarthome.model.script.runtime.internal.engine.ScriptImpl.execute(ScriptImpl.java:77)[115:org.eclipse.smarthome.model.script.runtime:0.8.0.201604051458]
at org.eclipse.smarthome.model.rule.runtime.internal.engine.ExecuteRuleJob.execute(ExecuteRuleJob.java:60)[113:org.eclipse.smarthome.model.rule.runtime:0.8.0.201604051458]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[93:org.eclipse.smarthome.core.scheduler:0.8.0.201604051458]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[93:org.eclipse.smarthome.core.scheduler:0.8.0.201604051458]

Any thoughts on what the problem might be?

Be aware that the Samples under https://github.com/openhab/openhab/wiki/JSON-Transformations are for openHAB1. As I don’t use openHAB2 yet, I can only guess:

Is JSONPATH a addon in OpenHAB2? If so, I think, you have to use a command different from transform()

For var test I would suggest to set it to String, like you did for the json var.
For the logInfo() I think, you wanted to log the test var?

thanks for the reply.

I guess this is one of the challenges of using OpenHAB2. I’ve probably now read every entry containing JSONPATH on the forum and the general web.

I’ve tested a billion ways. I’ve scoured my log files. I even pulled a new snapshot. Nothing!

If anyone on here is using OpenHAB2 and could provide a test rule for me to try on my setup to see if everything is actually setup properly I’d appreciate it.

For reference, here is the last test I tried that didn’t work

import org.jo da.time.*
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule “TEST”
when
Time cron “*/3 * * * * ?”
then

var String json = ‘{“sensors”:[{“value”:“6”}]}’
var String value = transform(“JSONPATH”, “$.sensors[0].value”, json).toString

logInfo(‘test’, json)
end

it always fails on the transform line

Hello!

There is already a bug report for it. https://github.com/openhab/openhab-distro/issues/100
I hope this helps.

Cheers Hannes

yeah thanks, I found that after this post, and I added some debug logs to the bottom of it

1 Like

I guess you rather want to log “value” and not “json”.

But I have good news: I have just successfully tested your rule on the very latest OH2 distro and it now works (see also here)!

1 Like

Thank you so much for fixing this. I just updated my install and it’s working!!!