ExpressionParser icon indicating copy to clipboard operation
ExpressionParser copied to clipboard

Escaping square brackets

Open fabienblink opened this issue 3 years ago • 0 comments

I am using expression-parser in combination with jsonpath-plus library to parse json paths and read the data out of a json object based on the term returned. Here is what my parser code looks like:

export function initialiseParser(workerObject: WokerObjectType): ExpressionParser {
  return init(formula, (term: string) => {
    if (term === '$NULL') {
      return '$NULL';
    } else if (term.startsWith('$VALUE:')) {
      const [, key] = term.split('$VALUE:');
      return getJsonData({ path: key, json: workerObject }) ?? '$NULL';
    } else {
      return '$NULL';
    }
  });
}

I am trying to parse the below expression: $VALUE:Worker_Data.Employment_Data.Worker_Job_Data[?(@.attributes['wd:Primary_Job'] === '1')].Position_Data.Business_Site_Summary_Data.Name

When running this through the parser, I can see the term evaluates to .Position_Data.Business_Site_Summary_Data.Name. The result I would like to achieve is to get out the full expression: Worker_Data.Employment_Data.Worker_Job_Data[?(@.attributes['wd:Primary_Job'] === '1')].Position_Data.Business_Site_Summary_Data.Name.

My gut feeling is that the formula language is recognizing the '[]' and parses it at something else but looking through the docs I am unclear as to what.

I wonder is someone could shed some light on this for me? If formula uses [] for something else, how can I escape the character when using it in the expression? Also open to other solutions. Please let me know if more details are required.

fabienblink avatar Jun 15 '22 15:06 fabienblink