Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using reserved word 'in' as key in jsonNode fails #317

Open
ck-c8y opened this issue Aug 30, 2024 · 2 comments
Open

Using reserved word 'in' as key in jsonNode fails #317

ck-c8y opened this issue Aug 30, 2024 · 2 comments

Comments

@ck-c8y
Copy link

ck-c8y commented Aug 30, 2024

Hi,

when I try to parse the json payload with a reserved keyword 'in'

	expression = "in";
	String jsonPayload = "{ \"in\":1}";
	System.out.println("Using JSON:\n" + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj));
	System.out.println("expression=" + expression);
	expr = Expressions.parse(expression);

an exception is thrown:


com.api.jsonata4java.expressions.ParseException: line 1:0 at [@0,0:1='in',<5>,1:0]: mismatched input 'in' expecting {'(', 'true', 'false', STRING, 'null', '[', '{', '$', '$$', '**', NUMBER, FUNCTIONID, '*', '/', '-', '%', VAR_ID, ID}

        at com.api.jsonata4java.expressions.Expressions.parse(Expressions.java:113)
        at com.mytest.jsonnata.Test.test2(Test.java:105)
        at com.mytest.jsonnata.Test.main(Test.java:17)
line 1:0 at [@0,0:1='in',<5>,1:0]: mismatched input 'in' expecting {'(', 'true', 'false', STRING, 'null', '[', '{', '$', '$$', '**', NUMBER, FUNCTIONID, '*', '/', '-', '%', VAR_ID, ID}

Whereas when I use 'ink' instead of 'in' the test works.

I tested with version 2.5.1

@wnm3
Copy link
Member

wnm3 commented Aug 30, 2024

I'm afraid this is due to the difference in parsing approach used by JSONata4Java where we have defined a language and as you've noticed, in is a reserved keyword. Below is the definition of the language (full details are in the MappingExpression.g4 file. I suspect the same is true for "or" and "and".

expr:
   ID                                                     # id
 | '*'                                                    # field_values
 | DESCEND                                                # descendant
 | DOLLAR                                                 # context_ref
 | ROOT                                                   # root_path
 | ARR_OPEN exprOrSeqList? ARR_CLOSE                      # array_constructor
 | OBJ_OPEN fieldList? OBJ_CLOSE					      # object_constructor
 | expr '.' expr                                          # path
 | (('%' '.')+ expr)                                      # parent_path
 | '%'                                                    # parent_path_solitary
 | expr '^' '(' (('>'|'<')? ID (',' ('>'|'<')? ID)*)* ')' # op_orderby
 | expr ARR_OPEN ARR_CLOSE                                # to_array
 | expr ARR_OPEN expr ARR_CLOSE                           # array
 | expr OBJ_OPEN fieldList? OBJ_CLOSE                     # object
 | VAR_ID (emptyValues | exprValues)                      # function_call
 | FUNCTIONID varList '{' exprList? '}'                   # function_decl
 | VAR_ID ASSIGN (expr | (FUNCTIONID varList '{' exprList? '}'))                   # var_assign
 | (FUNCTIONID varList '{' exprList? '}') exprValues                               # function_exec
 | op=(TRUE|FALSE)                                        # boolean
 | op='-' expr                                            # unary_op
 | expr op=('*'|'/'|'%') expr                             # muldiv_op
 | expr op=('+'|'-') expr                                 # addsub_op
 | expr op='&' expr                                       # concat_op
 | expr op=('<'|'<='|'>'|'>='|'!='|'=') expr              # comp_op
 | expr 'in' expr                                         # membership
 | expr 'and' expr                                        # logand
 | expr 'or' expr                                         # logor
 | expr '?' expr (':' expr)?                              # conditional
 | expr CHAIN expr                                        # fct_chain
 | regularExpression                                      # regular_expression
 | regularExpressionCaseInsensitive                       # regular_expression_caseinsensitive
 | regularExpressionMultiline                             # regular_expression_multiline
 | '(' (expr (';' (expr)?)*)? ')'                         # parens
 | VAR_ID                                                 # var_recall
 | NUMBER                                                 # number
 | STRING                                                 # string
 | 'null'                                                 # null
 ;

@ck-c8y
Copy link
Author

ck-c8y commented Sep 2, 2024

Thank you for the information on the language.
Is there a workaround possbile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants