Use selector expressions with the update
operator to specify the fields to update.
This matching selector can check for matches to any value within an object.
The following example updates a field at the root level and a field in a nested level using the value selector:Source
%dw 2.0
var myInput = {
"name": "Ken",
"lastName":"Shokida",
"age": 30,
"address": {
"street": "Amenabar",
"zipCode": "AB1234"
}
}
output application/json
---
myInput update {
case age at .age -> age + 1
case s at .address.street -> "First Street"
}
{
"name": "Ken",
"lastName": "Shokida",
"age": 31,
"address": {
"street": "First Street",
"zipCode": "AB1234"
}
}
This matching selector enables you to match any array by its index.
The following example updates an element that is at the first location of the array:Input
{
"name": "Ken",
"lastName":"Shokida",
"age": 30,
"addresses": [{
"street": "Amenabar",
"zipCode": "AB1234"
}]
}
%dw 2.0
output application/json
---
payload update {
case s at .addresses[0] -> {
"street": "Second Street",
"zipCode": "ZZ123"
}
}
{
"name": "Ken",
"lastName": "Shokida",
"age": 30,
"addresses": [
{
"street": "Second Street",
"zipCode": "ZZ123"
}
]
}
This matching selector enables you to match any attribute value.
The following example updates an attribute value and changes it to upper case:Input
<user name="leandro"/>
%dw 2.0
output application/json
---
payload update {
case name at .user.@name -> upper(name)
}
<?xml version='1.0' encoding='UTF-8'?>
<user name="LEANDRO"/>
Any selector can incorporate an expression to make the selection dynamic.
The following example dynamically selects and updates the value of a field that is not hardcoded. Such a field can change at runtime. The example uses the variable theFieldName
to obtain the field with key name
, and it changes the value of that field to Shoki
.Input
{
"name": "Ken",
"lastName":"Shokida"
}
%dw 2.0
var theFieldName = "name"
output application/json
---
payload update {
case s at ."$(theFieldName)" -> "Shoki"
}
{
"name": "Shoki",
"lastName": "Shokida"
}
The update
operator has syntax for writing a conditional update if you want to modify a field under a given condition.
The following example updates the category based on the name of the user:Input
[{"name": "Ken", "age": 30}, {"name": "Tomo", "age": 70}, {"name": "Kajika", "age": 10}]
%dw 2.0
output application/json
---
payload map ((user) ->
user update {
case name at .name if(name == "Ken") -> name ++ " (Leandro)"
case name at .name if(name == "Tomo") -> name ++ " (Christian)"
}
)
[
{
"name": "Ken (Leandro)",
"age": 30
},
{
"name": "Tomo (Christian)",
"age": 70
},
{
"name": "Kajika",
"age": 10
}
]
The update
operator enables you to insert a field that is not present already by using the !
symbol at the end of the selector expression. When the field is not present, the operator binds a null
value to the variable.
The following example updates each object in the array myInput
. If the field name
is present in the object, the example applies the upper
function to the value of name
. If the field name
is not present in the object, the example appends the key-value pair "name": "JOHN"
to the object.Source
%dw 2.0
var myInput = [{"lastName": "Doe"}, {"lastName": "Parker", "name": "Peter" }]
output application/json
---
myInput map ((value) ->
value update {
case name at .name! -> if(name == null) "JOHN" else upper(name)
}
)
[
{
"lastName": "Doe",
"name": "JOHN"
},
{
"lastName": "Parker",
"name": "PETER"
}
]
The update
operator accepts a default variable named $
in place of the longer <variable_name> at
syntax.
The following example binds the value of the expression .age
to the default variable name $
, which is used to express the new value. The longer equivalent is shown in comments.Input
{
"name": "Ken",
"lastName":"Shokida",
"age": 30,
"address": {
"street": "Amenabar",
"zipCode": "AB1234"
}
}
%dw 2.0
output application/json
---
payload update {
//equivalent expression:
//case age at .age -> age + 1
case .age -> $ + 1
case .address.street -> "First Street"
}
{
"name": "Ken",
"lastName": "Shokida",
"age": 31,
"address": {
"street": "First Street",
"zipCode": "AB1234"
}
}
If you like my post please follow me to read my latest post on programming and technology.
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…