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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…