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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 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
  • JSON Handling With GSON in Java With OOP Essence

Trending

  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  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
16.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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 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
  • JSON Handling With GSON in Java With OOP Essence

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!