DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Introduction to Polymorphism With Database Engines in NoSQL Using Jakarta NoSQL
  • Implementation Best Practices: Microservice API With Spring Boot

Trending

  • 8 Major Challenges Faced by Android Application Developers
  • Reproducible Development Environments, One Command Away: Introducing CodingBooth
  • Logging What AI Agents Do in Salesforce: A Simple One-Object Audit Framework
  • Implementing Secure API Gateways for Microservices Architecture
  1. DZone
  2. Coding
  3. Languages
  4. Expressing a Conditional Expression Using JSON: Java Implementation

Expressing a Conditional Expression Using JSON: Java Implementation

By 
Biju Kunjummen user avatar
Biju Kunjummen
·
Jul. 20, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
17.9K Views

Join the DZone community and get the full member experience.

Join For Free

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:

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


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

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


For equality, an expression using JsonLogic is the following:

JSON
xxxxxxxxxx
1
 
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:

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


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:

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


A check for equality looks like this:

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


Not operator:

JSON
xxxxxxxxxx
1
10
 
1
{
2
    "not": [
3
        {
4
            "equals": [
5
                "/a",
6
                "hello"
7
            ]
8
        }
9
    ]
10
}


And/Or operator:

JSON
xxxxxxxxxx
1
14
 
1
{
2
    "and": [
3
        {
4
            "equal": [
5
                "/a", "hello"
6
            ]
7
        },
8
        {
9
            "equal": [
10
                "/b", 1
11
            ]
12
        }
13
    ]
14
}


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

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


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

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


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
xxxxxxxxxx
1
 
1
implementation 'com.github.bijukunjummen:json-conditional-expression:0.4.0'


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

Kotlin
xxxxxxxxxx
1
 
1
val jsonExpressionEvaluator: JsonExpressionEvaluator = JsonExpressionEvaluator(ObjectMapper())
2
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

JSON Implementation Java (programming language)

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

Opinions expressed by DZone contributors are their own.

Related

  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Introduction to Polymorphism With Database Engines in NoSQL Using Jakarta NoSQL
  • Implementation Best Practices: Microservice API With Spring Boot

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook