Skip to content

How do I merge two or more JSON objects to a single object

This example demonstrates merging two or more JSON objects to a single object.

In the solution shown below, the two JSON objects are stored into the variables using "store" command and merged using "jsonMergeObjects" 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
Store the JSON object 1 in a variable
1 store {"age":45,"income":25000,"zipcodeDetails":[{"zipcode":90001,"state": "California"}]} JSON_1
Store the JSON object 2 in a variable
2 store
{"result":"You are eligible for a subsidy."}
JSON_2
Merge the JSON objects and store the result in a variable
3 jsonMergeObjects
${JSON_1} || ${JSON_2}
Final_JSON

Tips, Tricks, Gotchas & Best Practices:  

  • The following rules are applied while merging the JSON objects:
    • Adjacent arrays are merged to a single array.
    • Adjacent objects are merged to a single object.
    • A scalar value is autowrapped as an array and merged as an array.
    • An adjacent array and object are merged by autowrapping the object as an array and merging the two arrays.
  • You can also merge more than two objects by providing the JSON objects in the "Target" field using the separator "||".
  • You can also call both the JSON objects directly in the "Target" field separated by "||" symbol in "jsonMergeObjects" 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