user1477232
user1477232

Reputation: 457

Using JSONata in Java throws exception

I am trying to use JSONata in java.

JSONata version - 1.4.0
Java Version - 1.8

Steps: Included Jsonata.js in the class path.

Invocation:

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
Invocable inv = (Invocable) engine;
FileReader jsonata = new FileReader("jsonata.js");
// load the JSONata processor
engine.eval(jsonata);

Reference link: How can I use JSONata in Java?

When I try to use this i get the below exception:

Caused by: jdk.nashorn.internal.runtime.ParserException: :1445:12 Expected ( but found function evaluate(expr, input, environment) {

at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:294) at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:279) at jdk.nashorn.internal.parser.AbstractParser.expectDontAdvance(AbstractParser.java:350) at jdk.nashorn.internal.parser.AbstractParser.expect(AbstractParser.java:337) at jdk.nashorn.internal.parser.Parser.functionExpression(Parser.java:2654) at jdk.nashorn.internal.parser.Parser.statement(Parser.java:875) at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:773) at jdk.nashorn.internal.parser.Parser.functionBody(Parser.java:2901) at jdk.nashorn.internal.parser.Parser.functionExpression(Parser.java:2663) at jdk.nashorn.internal.parser.Parser.memberExpression(Parser.java:2506) at jdk.nashorn.internal.parser.Parser.leftHandSideExpression(Parser.java:2372) at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3147) at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3282) at jdk.nashorn.internal.parser.Parser.primaryExpression(Parser.java:1992) at jdk.nashorn.internal.parser.Parser.memberExpression(Parser.java:2511) at jdk.nashorn.internal.parser.Parser.leftHandSideExpression(Parser.java:2372) at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3147) at jdk.nashorn.internal.parser.Parser.assignmentExpression(Parser.java:3353) at jdk.nashorn.internal.parser.Parser.variableStatement(Parser.java:1088) at jdk.nashorn.internal.parser.Parser.statement(Parser.java:884) at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:773) at jdk.nashorn.internal.parser.Parser.program(Parser.java:709) at jdk.nashorn.internal.parser.Parser.parse(Parser.java:283) at jdk.nashorn.internal.parser.Parser.parse(Parser.java:249) at jdk.nashorn.internal.runtime.Context.compile(Context.java:1284) at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1251) at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:627) at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:535) ... 6 moree

Any help on how to use it will be useful.

Upvotes: 0

Views: 891

Answers (4)

wnm3
wnm3

Reputation: 421

There is now a Java port of the JSONata JavaScript code. Please see this question's answers for details: How can I use JSONata in Java?

Upvotes: 0

MTH
MTH

Reputation: 161

I confirm that earlier versions of JSONata function with the nashorn engine in java version "1.8.0_152" using either the link to jsonata-es5.min.js provided by Andrew or jsonata.git/tags/v1.1.1/jsonata.js from the jsonata repostiory.

Upvotes: 0

Andrew Coleman
Andrew Coleman

Reputation: 1559

Since v1.2, jsonata.js uses ES6 features, so you'll need to make sure the version of Nashorn you are using supports this. Alternatively, you can use the jsonata-es5.js version that gets generated by Babel in the JSONata build. You can get this either by cloning the GitHub repo and running npm t, or by downloading it from here.

Upvotes: 1

Leroy
Leroy

Reputation: 472

You need to provide the function Evaluate with three parameters, You only given the expression . Based on the link you should also provide the ff

Code :

engine.put("input", new String(sample));
and 
engine.put("resultjson", resultjson);

Hope it helps

Upvotes: 0

Related Questions