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.
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
You can open up a window to evaluate JSONPath expressions by going to Edit -> Find -> "Evaluate JSONPath Expression...". If a JSON file is open, it will use this file to evaluate the expression. If you have JSONPath expressions as Strings in code, use "inject language" and say this is a JSONPath expression.
In order to check the validity of a string whether it is a JSON string or not, We're using the JSON. parse()method with few variations. This method parses a JSON string, constructs the JavaScript value or object specified by the string.
try JsonPath.compile("$.path")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With