JSONata is obscure[1] but beloved within Node-RED community because it offers simple expressions for JSON constructs and also for message manipulation.
To quote from the the JSONata website:
Lightweight query and transformation language for JSON data
Inspired by the location path semantics of XPath 3.1
Sophisticated query expressions with minimal syntax
Built in operators and functions for manipulating and combining data
Create user-defined functions
Format query results into any JSON output structure
JSONata - once understood - is very helpful in the context of visual low-code programming since it provides functionality that otherwise would be done using something like a function node (i.e. direct code) or other codification.
Besides that, the Node-RED has a very good integration and testing environment for JSONata, so that by the time it hits the server, it will just work.
Ok, so my question is: does anyone have any tips how to best implement something like JSONata in Erlang? Any help much appreciated!
Cheers!
[1]: I donât think Iâm offending anyone by saying, apologise if I do.
From the looks of it, JSONata is some sort of JSON transformation DSL. We maintain Erlang bindings for JQ which does something similar: GitHub - emqx/jq: jq (nif) for beam
Thanks for the tip and indeed they both do the same thing, just slightly differently
For example JSONata uses â$â where jq uses â.â - but not always. jq seems to overload the dot to mean either variable or hashmap key separator, while JSONata uses dot solely as hashmap key separator.
The problem is that JSONata is very deeply integrated into the frontend of Node-RED so there is no realistic possibility of replacing it. Transforming JSONata code to jq also seems unrealistic because of their differences, i.e, naively doing a search and replace of all dollars with dots and out comes jq code!
Iâll had look and yecc and leex and Iâll have a go at creating a simple parser for basic JSONata code - most of the usage of JSONata within Node-RED are very simple constructs and not as complex as generating a uuid with JSONata.
For those interested, I ended up in rolling my own parser since there wasnât a grammer file to be found. It seems the original parser is handed coded.
I read up on leex and yecc and came up with these two jsonata_leex and jsonata_yecc which sort of do the job, at least they cover most of my use cases.
What I decided on was to convert JSONata code to Erlang code and the Erlang code is evaluated in the presence of a msg object/map. In the long term all JSONata stanzas would be precompiled upon deployment and then directly executed on the message objects - hence the conversion to Erlang.
Of course I am nowhere near covering all possibilities of JSONata (my parser canât even add 1 plus 1 - forgot I needed that use-case) but at least there is a start!
Is there somewhere a specification available apart documentation , like BNF grammar ? Canât find anything useful on home site.
âWhat is undocumented is simply not existingâ.