0

I have a JSON document from which I have figured a way of parsing and extracting the values within it using the library of JSON-Path

I have to evaluate rules/expressions using the variables in the parsed JSON.

For example:

{maxAge:25, minAge:20, age:25}

  • rule1 : $.maxAge-$.minAge>100 then send alert
  • rule2 : $.age<18 then send alert

What libraries are available in Java for the same ?

Aniketh Jain
  • 603
  • 7
  • 25
Mahesh Kumar
  • 138
  • 1
  • 9

3 Answers3

0

Json Path will only let you parse the JSON file and read the variables within it. You have to use a separate Java library for evaluating the expressions. Take a look at a this post which discuss the same.

Another useful post that I found on purely mathematical expression evaluation is this one.

It would be helpful if you can identify all the operators that will be part of the expression. For ex, Arithmetic Operators ( +, -, /, *. ), Equality and Relational Operators ( =, !=, <, > )

Aniketh Jain
  • 603
  • 7
  • 25
0

There are several java libraries that allow you to parse JSON. Two major ones are Jackson-JSON and Gson. Both are de-facto standard. My personal preference is Jackson.

Here is the simplest way to parse JSON:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModules(new JavaTimeModule());
ObjectReader objectReader= objectMapper.reader();
Map<String, Object>data = objectReader.forType(Map.class).readValue(s);

Here are the needed maven artifacts

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.12.3</version>        
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.12.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
  <version>2.12.3</version>
</dependency>

Here is github site: https://github.com/FasterXML/jackson.
Here is relevant Javadoc

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

Figured out a way, apparently, I don't need a JSON path. I can use javascript directly inside my code. so I've written rules in javascript

public static void main(String[] args) {
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
        String json = "{\"maxAge\":25, \"minAge\":20, \"age\":25}";
        String rule = "(x.maxAge)-(x.minAge) > 0;";

        jsEngine.put("x", json);

        try {
            jsEngine.eval("function test(abc){" + "var x=JSON.parse(abc);" + "return " + rule + "}");

            Invocable inv = (Invocable) jsEngine;
            boolean result = (Boolean) inv.invokeFunction("test", json);
            if (result) {
                System.out.println("Alert rule triggered");
            }

        } catch (ScriptException ex) {
            ex.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }
Mahesh Kumar
  • 138
  • 1
  • 9