Skip to content

How do I get the maximum depth of the JSON object and length (size) of an array for the defined path in the JSON object

This example demonstrates getting the maximum depth of the JSON object and length (size) of an array for the defined path in the JSON object.

In the solution shown below, the JSON object and the JSON Path are stored into the variables using "store" command. The maximum depth of the JSON is fetched using "jsonGetDepth" command and the length of an array for the defined path in the JSON object is fetched using "jsonGetLength" command.

Note: The notation to reuse the value of the variable in Worksoft SaaS is ${<Variable Name>}.

Solution:

# Command Target Value
Store the JSON object in a variable
1 store {"zipcodeDetails":[{"zipcode":90001,"state": "California"},{"zipcode":10001,"state": "New York"}]} JSON
Get the Depth of the JSON and store it in a variable
2 jsonGetDepth
${JSON}
Depth
Store the JSON Path in a variable
3 store
$.zipcodeDetails
JSONPath
Get the Length of the array in JSON Object for the defined path and store it in a variable
4 jsonGetLength
${JSON}||${JSONPath}
Lenght

Tips, Tricks, Gotchas & Best Practices:  

  • You can also call JSON and JSON Path directly in the "Target" field in "jsonGetDepth" and "jsonGetLength" command 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.


Feedback and Knowledge Base