Skip to content

How do I insert data into a JSON object

This example demonstrates inserting data into a JSON object.

In the solution shown below, the JSON object, JSON path and value are stored into the variables using "store" command. A new key "age" is included into the JSON object by adding it in the JSON path and value for the key is inserted into the JSON object using "jsonInsertIntoObject" 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 in a variable
1 store {"insuranceDetails":{"income": 25000,"zipcodeDetails": [{"zipcode": 90001,"state": "California"}]}} JSON
Store the JSON path in a variable
2 store
$.insuranceDetails.age
JSONPath
Store the value for the key "age" in the JSON object
3 store
45 Value
Insert the value for the defined path into the JSON object and store the result in a variable
4 jsonInsertIntoObject
${JSON}||{${JSONPath},${Value}} Final_JSON

Tips, Tricks, Gotchas & Best Practices:  

  • If the given path does not exist in the JSON object, the value will be added only in following cases:
  • A member is not present in an existing object - The member is added to the object and associated with the new value.
  • A position past the end of an existing array - The array is extended with the new value. If the existing value is not an array, it is auto wrapped as an array, then extended with the new value. Otherwise, a path/value pair for a nonexistent path in the document is ignored and has no effect.
  • You can also call the JSON, JSON path and value directly in the "Target" field separated by "||" symbol in "jsonInsertIntoObject" 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