How do I retrieve values from JSON object using "index" or "name" as reference and store it in a variable
This example demonstrates how to retrieve values from JSON using "index" or "name" as reference and store it in a variable using "jsonStoreArrayByIndex" or "jsonStoreArrayByName" commands respectively.
In the solution shown below, the json object is stored into a variable using "store" command. The values from the json object are retrieved by "index" using "jsonStoreArrayByIndex" command and by "name" using "jsonStoreArrayByName" command and stored into a variable.
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 | { "age": 64, "income": [25000, 45000, 55000] } | JSON |
Retrieve values from JSON using index and stores it in a variable | |||
2 | jsonStoreArrayByIndex |
${JSON}||1 |
Index |
Retrieve values from JSON using name and stores it in a variable | |||
3 | jsonStoreArrayByName |
${JSON}||income | Name |
Tips, Tricks, Gotchas & Best Practices:
- You can also call JSON directly in the target field 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.