Skip to content

How do I append the values to an array within the JSON object

This example demonstrates appending the values to an array within the JSON object.

In the solution shown below, the JSON object, JSON path and value are stored into the variables using "store" command. A value is appended to an array for the defined path within the JSON object using "jsonAppendToObject" command. The result is stored into a variable.

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

Solution:

# Command Target Value
Storing the JSON object in a variable
1 store {"zipcodeDetails": {"NewJersey": ["08034","08540"]}} JSON
Store the JSON path in a variable
2 store $.zipcodeDetails.NewJersey
JSONPath
Store the value to be added to the array in the JSON object
3 store 08817 Value
Append the value for the defined path in the JSON object and store the result in a variable
4 jsonAppendToObject
${JSON}||{${JSONPath},${Value}}
Final_JSON

Tips, Tricks, Gotchas & Best Practices:  

  • You can also call the JSON, JSON path and value directly in the "Target" field separated by "||" symbol in "jsonAppendToObject" 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