Skip to content

How do I replace existing values in the JSON object by path

This example demonstrates replacing existing values in the JSON object by path.

In the solution shown below, store the JSON object, JSON path and new value are stored into separate variables using "store" command. An existing value is replaced with a new value at the specified path within the JSON object using "jsonReplaceValuesByPaths" command. The result is stored into a new variable.

Solution:

# Command Target Value
Storing the JSON object in a variable
1 store {"age":45,"income":25000,"zipcodeDetails":[{"zipcode":90001,"state": "California"}]} JSON
Store the JSON path in a variable
2 store
$.income
JSONPath
Storing the new key to be replaced in the JSON object
3 store
20000 Value
Replacing the existing value for the defined path in the JSON object with new value and storing the result in a variable
4 jsonReplaceValuesByPaths
${JSON}||{${JSONPath},${Value}}
Final_JSON

Tips, Tricks, Gotchas & Best Practices:  

  • You can also replace multiple values by providing multiple {path, value} combinations separated by "||" symbol.
  • You can call the JSON, JSON path and value directly in the "Target" field separated by "||" symbol in "jsonReplaceValuesByPaths" 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