Thursday, 29 July 2021

Parsing a Stanadrd Query into JSON Clause Tree

I want to build a query for my system that could be used by external systems for configuration based on conditions.

On the backend I find it easy to have a JSON Clause tree which would be evaluated recursively.

[
  "AND",
  [
    {
      "operator": "eq",
      "field": "section1.fieldabc",
      "value": "value1"
    },
    [
      "OR",
      {
        "operator": "lt",
        "field": "section2.fieldxyz",
        "value": 5000
      },
      {
        "operator": "gt",
        "field": "section2.fieldxyz",
        "value": 1000
      }
    ]
  ]
]

or something similar. (Above I have represented it something like an s-expression tree)

The thing is I want it as a JSON Clause Tree in Backend but I don't want the users to need to write anything like this. It would be great if I can create a query something like JQL (Jira query language) or something. But I don't want to spend alot of effort actually making a full proof parser for the language that would convert.

Is there any standardised way to implement this? Maybe a standardised query language which gets converted using a library (In js or Java)

From the end users perspective i want the above query to be like

section1.fieldabc == value1 AND (section2.fieldxyz<5000 OR section2.fieldxyz>10000)


from Parsing a Stanadrd Query into JSON Clause Tree

No comments:

Post a Comment