Update Json
Adds or edits values of JSON by provided path
Description
Adds or edits values of JSON by specified by the property path.
The path is specified in dotted form. To update or add an element inside an array, the property needs to be included inside brackets, e.g. '[name]'.
If the path was found the value of the property will be replaced by a new value. If it was not found the new value will be added to the source json.
Examples:
Adding a simple primitive value to the root:
UpdateJSON( [ '{"a":"test","b":{}}', 'c', 42 ] ) ->{"a":"test","b":{},"c":42}
Replacement of a property in the array by a new object:
UpdateJSON( {"a": "test", "b": [ { "b1": "foo"}, { "b1": "foo" } ] }, b.[b1], "test 2") -> {"a": "test", "b": [ { "b1": "test 2"}, { "b1": "test 2"}]}
Adding a new object to the property inside an object in array that already contains a primitive value:
UpdateJSON({ "type" : "event", "desc": [ { "year": "1900"}, { "year": "1991" } ] }, 'desc.[year].month', { "day": "14th of May" } ) ->
{"type":"event" , "desc" : [ { "year" : { "1900" : "1900", "month" : { "day": "14th of May" } } }, { "year" : { "1991" : "1991", "month" : { "day": "14th of May" } } } ] }
It is possible to add a new value to the last unknown property. If one needs to add a new value deeper than to the first non-existing property, it is necessary to put it as a wrapper in the new value, e.g.
Source: {"country": "Germany", "store": {"author": "Goethe"}} Desired path: store.category.books Desired new value: { "name": "fiction" }
Actual path: store.category Actual new value: { "books" : { "name": "fiction" } }
Inputs
- Control Flow (Control Flow)
- Source (Object): JSON object or string
- Path (String): The property path
- New Value (Object): The value to be added or use for replacing
Outputs
- Control Flow (Control Flow)
- Result (Object): Transformed JSON object
- Error Flow (Error Flow): Triggered by providing incorrect json