Creating a regex to validate whether or not a string is a legit JSONata expression would be quite difficult and likely not very reliable. JSONata expressions can get really complex, and trying to account for all the different variations in a single regex pattern would be almost impossible.
Instead of going down that road, it's better to utilize the JSONata library for evaluating the expression and handling any potential exceptions. If an exception is thrown, then you can assume that the JSONata expression entered is invalid.
Here's a straightforward example using JavaScript along with the JSONata library:
const jsonata = require('jsonata');
function checkJSONataValidity(expression) {
try {
jsonata(expression);
return true;
} catch (error) {
return false;
}
}
// Example of how to use this function
const expr = 'your JSONata expression here';
if (checkJSONataValidity(expr)) {
console.log('The expression is valid');
} else {
console.log('The expression is invalid');
}
This method offers a more precise and sustainable way to verify the validity of a JSONata expression.
If you're not keen on dealing with code and simply want to experiment with some expressions, you can explore the JSONata Playground.