A parser for JSON Boolean query
Hi, it is my (or our) very first day of the Lunar New Year, year of Tiger. To embrace the energy, push myself over the limit. I want to renovate one of my old toolkit I’ve made for my team.
I bet that you’ve heard about it some times about a Boolean query that made by JSON, including me. I also inspired by one of my old teammate and some gists from Github. Talking about the idea, why I had to search and make our own JSON Boolean query, our team are making products on smart health industry, one of those is bringing tools that aid the diagnosis by computer to patient’s DICOM. More information about the DICOM standard, please visit the link.
To make it quickly, I will try to mention medical’s terms as little as possible. DICOM provides metadata of ‘a shot‘ for a specific patient like which technique is used, which body part is examined, which modality is being focused… They can be generated to JSON. Let’s say we have an input JSON like that. And from now on, you should concentrate only on this.
I can say Object
field have various of value but they are limited like: CAR, BIKE, CAT, DOG; ObjectDescription
is an array of free strings and ObjectTags
is represented as an array of enums... any fields or values you expected to compare (All of these values are arrays).
Basic requirements are:
- an expression may contain two or more nested smaller expressions
- an element
Let’s make an expression: ObjectType equal to CAR and ObjectTags in FOO and ObjectDescription contains 'CAR' and (ObjectBodyPart equal to 'HOOD' and ObjectTags in ['CAR', 'OTO'])
. Based on these requirements
To parse the above expression, I made the following code. To make sure that everything work well, I convert the value to uppercase, but you can modify base on demand.
Today is 02/02/2022, a groundhog day. Happy Lunar New Year, cheers!