Reputation: 2255
Hi I am trying to find a way to validate jsonpath entered by user before evaluating it. I was hoping use something like a regex to do that but so far I could not find any doc/resource on how to validate jsonpath syntax.
All searched return resources that talk about evaluating the expression. Even the jayway.JsonPath library does not seem to do any syntax check.
Is it not possible to do a syntax check for jsonpath syntax ? If it is possible can you please point me in the right direction.
Upvotes: 8
Views: 7064
Reputation: 400
I tried to create a JSON path expression validator based off of Stefan Goessner's article and JS implementation.
The code is hosted on the Github repository at jsonpath-syntax-validator
Following is a sample usage of the library. It doesn't evaluate against a JSON object –– it's a strictly superficial check. However, in my opinion Jayway's JsonPath would be a better choice in case evaluation of the path expression is needed.
JsonPathValidator validator = new BasicJsonPathValidator();
Assert.assertTrue(validator.validate("$.as[?(@.name == 'samba')]"));
Assert.assertFalse(validator.validate("a['??kangaroo \"A\"'][??(@.name == 'a')].value"));
References:
Upvotes: 0
Reputation: 196
Depending on the language you are using, you could use try-catch, but since you have said you are looking for a specific error message, you should check if the function returns an error message to check. You can also try taking a look at this GitHub project by ashphy. It is JSONPath validator that may work for you.
Upvotes: 0