How do I validate whether the given path exists within a JSON
This example demonstrated validating a path using "jsonAssertPathExists" command.
In the solution shown below, the JSON object and the JSON Path are stored into the variables using "store" command and the JSON Path is validated using "jsonAssertPathExists" command. The command returns "1" if the path exists in the JSON object, else it returns "0".
Note: The notation to reuse the value of the variable in Worksoft SaaS is ${<Variable Name>}.
Solution:
# | Command | Target | Value |
---|---|---|---|
Store the JSON in a variable | |||
1 | store | {"zipcodeDetails":[{"zipcode":90001,"state": "California"},{"zipcode":10001,"state": "New York"}]} | JSON |
Store the JSON Path in a variable | |||
2 | store |
$.zipcodeDetails[0].zipcode |
JSONPath |
Validate the JSON path and store the result in a variable | |||
3 | jsonAssertPathExists |
${JSON}||${JSONPath} |
JsonPathExists |
Tips, Tricks, Gotchas & Best Practices:
- You can also call JSON and JSON Path directly in the target field separated by || symbol to validate the JSON path existence in the JSON object instead of storing them into a variable.
- As a best practice, it is always advisable to define the variable names without any spaces and if you want to differentiate the words, use underscores.