Expressing a Conditional Expression Using JSON: Java Implementation

DZone 's Guide to

Expressing a Conditional Expression Using JSON: Java Implementation

I had a need to express a conditional in a form that a JavaScript front-end and a Java back-end could both create and read. JSON felt logical.

· Java Zone ·
Free Resource

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 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:


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


For equality, an expression using JsonLogic is the following:


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:


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:


A check for equality looks like this:


Not operator:


And/Or operator:


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


or to check if the collection has any of the elements "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):

Plain Text

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



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.


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

java, json, jsonlibrary, tutorial

Published at DZone with permission of Biju Kunjummen , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}