Contact Us 1-800-596-4880

update

update(objectValue: Object, fieldName: String): UpdaterValueProvider<Object>

This update function updates a field in an object with the specified string value.

The function returns a new object with the specified field and value.

Introduced in DataWeave version 2.2.2.

Parameters

Name Description

objectValue

The object to update.

fieldName

A string that provides the name of the field.

Example

This example updates the name field in the object {name: "Mariano"} with the specified value.

Source

%dw 2.0
import * from dw::util::Values
output application/json
---
{name: "Mariano"} update "name" with "Data Weave"

Output

{
  "name": "Data Weave"
}

update(objectValue: Object, fieldName: PathElement): UpdaterValueProvider<Object>

This update function updates an object field with the specified PathElement value.

The function returns a new object with the specified field and value.

Introduced in DataWeave version 2.2.2.

Parameters

Name Description

objectValue

The object to update.

fieldName

A PathElement that specifies the field name.

Example

This example updates the value of a name field in the object {name: "Mariano"}. It uses field("name") to return the PathElement that it passes to update. It uses with "Data Weave" to specify the value (Data Weave) of name.

Source

%dw 2.0
import * from dw::util::Values
output application/json
---
{name: "Mariano"} update field("name") with "Data Weave"

Output

{
  "name": "Data Weave"
}

update(arrayValue: Array, indexToUpdate: Number): UpdaterValueProvider<Array>

Updates an array index with the specified value.

This update function returns a new array that changes the value of the specified index.

Introduced in DataWeave version 2.2.2.

Parameters

Name Description

objectValue

The array to update.

indexToUpdate

The index of the array to update. The index must be a number.

Example

This example replaces the value 2 (the index is 1) with 5 in the the input array [1,2,3].

Source

%dw 2.0
import * from dw::util::Values
output application/json
---
[1,2,3] update 1 with 5

Output

[
   1,
   5,
   3
 ]

update(arrayValue: Array, indexToUpdate: String): UpdaterValueProvider<Array>

This update function updates all objects within the specified array with the given string value.

Introduced in DataWeave version 2.2.2.

Parameters

Name Description

objectValue

The array of objects to update.

indexToUpdate

A string providing the name of the field to update.

Example

This example updates value of the role fields in the array of objects.

Source

%dw 2.0
import * from dw::util::Values
output application/json
---
[{role: "a", name: "spiderman"}, {role: "b", name: "batman"}] update "role" with "Super Hero"

Output

[{
   "role": "Super Hero",
   "name": "spiderman"
 },
 {
   "role": "Super Hero",
   "name": "batman"
}]

update(arrayValue: Array, indexToUpdate: PathElement): UpdaterValueProvider<Array>

This update function updates the specified index of an array with the given PathElement value.

The function returns a new array that contains given value at the specified index.

Introduced in DataWeave version 2.2.2.

Parameters

Name Description

objectValue

The array to update.

indexToUpdate

The index of the array to update. The index must be specified as a PathElement.

Example

This example updates the value of an element in the input array. Notice that it uses index(1) to return the index as a PathElement, which it passes to update. It replaces the value 2 at that index with 5.

Source

%dw 2.0
import * from dw::util::Values
output application/json
---
[1,2,3] update index(1) with 5

Output

[
   1,
   5,
   3
 ]

update(value: Array | Object | Null, path: Array<String | Number | PathElement>): UpdaterValueProvider<Array | Object | Null>

Updates the value at the specified path with the given value.

Introduced in DataWeave version 2.2.2.

Parameters

Name Description

objectValue

The value to update. Accepts an array, object, or null value.

path

The path to update. The path must be an array of strings, numbers, or `PathElement`s.

Example

This example updates the name of the user.

Source

%dw 2.0
import * from dw::util::Values
output application/json
---
{user: {name: "Mariano"}} update ["user", field("name")] with "Data Weave"

Output

{
   "user": {
     "name": "Data Weave"
   }
 }

Example

This example updates one of the recurring fields.

Input

<users>
  <user>
    <name>Phoebe</name>
    <language>French</language>
  </user>
  <user>
    <name>Joey</name>
    <language>English</language>
  </user>
</users>

Source

%dw 2.0
import * from dw::util::Values
output application/xml
---
payload update ["users", "user", "language"] with (if ($ == "English") "Gibberish" else $)

Output

<users>
  <user>
    <name>Phoebe</name>
    <language>French</language>
  </user>
  <user>
    <name>Joey</name>
    <language>Gibberish</language>
  </user>
</users>

update(value: Null, toUpdate: Number | String | PathElement): UpdaterValueProvider<Null>

Helper function that enables update to work with a null value.

Introduced in DataWeave version 2.2.2.