react-querybuilder/react-querybuilder

Support for JSONata

Closed this issue ยท 8 comments

Thank you for this library, it is great and I appreciate all the effort that has gone into it!

Description of the feature

JSONata is a JSON query and transformation library. It seems to support the conditions and operators that this library supports. As a suggestion, it would be great to be able to import and export JSONata.

JSONata supports returning an AST. There is additional library that looks to support serializing a JSONata AST back into a JSONata expression. I think these could aid in adding support for JSONata.

Use case

The use case is to support an additional query language for import and export.

Thanks for the kind words! This looks interesting, but it seems very much focused on the "select" part of a query, while the "where" part is sort of intermingled instead of independent like a SQL WHERE clause. RQB doesn't really handle the "select" part at all (this is the reason RQB doesn't natively support import/export of GraphQL).

That said, if you can provide some examples of JSONata expressions along with the corresponding RQB query objects you would expect from an import (and vice versa with export), I would definitely take another look.

Hi @jakeboone02, that's a fair point, the only way this could work is if there's a subset of the JSONata language supported.

Here's what I was imagining, using the example from https://react-querybuilder.js.org/

The JSONata expression would look something like this (link to JSONata Exerciser):

($contains(firstName, /^Stev/) and lastName in ['Vai', 'Vaughan'] and age > 28 and (isMusician = true or instrument = 'Guitar') and groupedField1 = groupedField4 and $toMillis('1954-10-03') < $toMillis(birthDate) and $toMillis(birthDate) < $toMillis('1960-06-06'))

and the corresponding RQB JSON (I think that's what you were asking about?) would be this (taken from the https://react-querybuilder.js.org/ page).

{
  "combinator": "and",
  "not": false,
  "rules": [
    {
      "field": "firstName",
      "value": "Stev",
      "operator": "beginsWith"
    },
    {
      "field": "lastName",
      "value": "Vai, Vaughan",
      "operator": "in"
    },
    {
      "field": "age",
      "operator": ">",
      "value": "28"
    },
    {
      "combinator": "or",
      "rules": [
        {
          "field": "isMusician",
          "operator": "=",
          "value": true
        },
        {
          "field": "instrument",
          "operator": "=",
          "value": "Guitar"
        }
      ]
    },
    {
      "field": "groupedField1",
      "operator": "=",
      "value": "groupedField4",
      "valueSource": "field"
    },
    {
      "field": "birthdate",
      "operator": "between",
      "value": "1954-10-03,1960-06-06"
    }
  ]
}

Ah, now we're getting somewhere! I'll take a stab at an export (i.e., formatQuery(query, 'jsonata')) first. FWIW the imports (parse* methods) pretty much all support only a subset of their respective formats, so doing that with JSONata would be in keeping with the norm.

That would be amazing! Thanks!

@KevinOBrienCimpress I made a first pass at a JSONata export format. A preview of the website is here, where you can see formatQuery(query, 'jsonata') in action in the demo. Preview documentation is here. Let me know what you think!

The only thing I wanted to implement but couldn't was date handling. RQB doesn't have an official way to determine when a rule's value should be treated like a date, so I couldn't use $toMillis(...) based on generic criteria. The documentation has an example custom rule processor where you can add your own date logic, and the same example is used in the live tests.

As for a parser/import method, I'll take that on as a longer term goal. We should probably put in a separate feature request for it because #684 will close this one when it's merged.

@jakeboone02 thanks for doing this!
The preview site looks good and so does the documentation!

I've created #685 for the separate/longer-term feature request.

(@KevinOBrienCimpress is swapping off our project and asked me to take over the communication on this)

That's great, @jnallard. I'll go ahead and merge the PR. Probably release 7.2.0 this week sometime.

v7.2.0 was released yesterday including the "jsonata" export format.