Enterprise Java

Expressing a conditional expression using Json – Java Implementation

I had a need recently to express a conditional expression in a form that a front end Javascript application and a backend Java application could both create and read. Expressing the conditional expression as a Json felt logical and after a quick search,
JsonLogic library appeared to fit exactly what I was looking for.

JsonLogic follows a prefix notation for its expressions, along these lines:

1
{"operator" : ["values" ... ]}

So for eg, given a JSON input data that looks like this:

1
2
3
4
5
6
7
8
{
  "a": "hello",
  "b": 1,
  "c": [
    "elem1",
    "elem2"
  ]
}

For equality, an expression using JsonLogic is the following:

1
{"==" : [ { "var" : "a" }, "hello" ] }

Here the data is being looked up using “var” expression and the equality is checked using the “==” operator.

Though it is a good fit, I decided to go with an alternate way of expressing the conditional expression but heavily inspired by JsonLogic. So, in my implementation, equality with the sample JSON looks like this:

1
2
3
4
5
{
    "equals": [
        "/a", "hello"
    ]
}

Fairly similar, the location to the data is expressed as a
Json Pointer and the operators are textual (“equals” vs “==”) The full set of supported features are also much smaller than JsonLogic as that sufficed my needs for the project. So now I have a small
Java-based library that supports these simplified conditional expressions and this post will go into the details of the operators and the usage of the library.

Sample Expressions

Once more to touch on the sample conditional expressions, everything takes the form of:

1
{"operator" : ["values" ... ]}

A check for equality looks like this:

1
2
3
4
5
{
    "equals": [
        "/a", "hello"
    ]
}

Not operator:

01
02
03
04
05
06
07
08
09
10
{
    "not": [
        {
            "equals": [
                "/a",
                "hello"
            ]
        }
    ]
}

And/Or operator:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
{
    "and": [
        {
            "equal": [
                "/a", "hello"
            ]
        },
        {
            "equal": [
                "/b", 1
            ]
        }
    ]
}

There are a few operators that work on collections, for eg, to check if “c” in the sample JSON has elements “elem1”, “elem2”:

1
2
3
4
5
{
    "contains": [
        "/c", ["elem1", "elem2"]
    ]
}

or to check if the collection has any of the elements “elem1”, “elem2”:

1
2
3
4
5
{
    "containsAnyOf": [
        "/c", ["elem1", "elem2"]
    ]
}

Details of the Library

The
Java-based library is built on top of the excellent
Jackson JSON parser library and uses it to parse the expression which once parsed is interpreted by the library. A Gradle based project can pull in the dependency the following way(
published currently to JCenter):

1
implementation 'com.github.bijukunjummen:json-conditional-expression:0.4.0'

and use the library along these lines, using a sample Kotlin code:

1
2
val jsonExpressionEvaluator: JsonExpressionEvaluator = JsonExpressionEvaluator(ObjectMapper())
jsonExpressionEvaluator.matches(expression, json) //returns true

Conclusion

The conditional expression and the corresponding Java-based interpreter are fairly simple and have sufficed my needs with the kind of operators that I needed for my project, however, I will be more than happy to extend and support more extensive operators if there is enough interest in using the library.

Reference

1.
JsonLogic which provided the inspiration for using a prefix notation to represent a conditional expression

2. The project resides in GitHub
here
https://github.com/bijukunjummen/json-conditional-expression

Published on Java Code Geeks with permission by Biju Kunjummen, partner at our JCG program. See the original article here: Expressing a conditional expression using Json – Java Implementation

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button