I have a payload as follows:
{
"relatedParty": [
{
"id": "001",
"Role": "Account"
},
{
"id": "006",
"Role": "OpportunityId"
}
]
}
and a dataweave expression to extract a value
payload..relatedParty[0][?($.Role=='Account')].id[0]
I just want to understand what does the following mean:
[?($.Role=='Account')]
Coming from a C#/.net background, I am still trying to wrap my head around this. Appreciate the help.
Rather than answering directly the question it could be valuable to understand first how to reach that answer.
The input is an object, ie it has a set of key-values. The descendants selector ..
returns an "Array of values of any matching descendant keys". For this input it doesn't make much sense, because you just say payload.relatedParty
. I'll assume other payloads could have the same key nested.
The output of payload..relatedParty
is:
[
[
{
"id": "001",
"Role": "Account"
},
{
"id": "006",
"Role": "OpportunityId"
}
]
]
Which as expected is an array, of only one element, since the input has only one instance of the key searched. Now we get the first element of the array with the subscript [0]
. Note that this will fail if the key is not found, but for this input it will succeed.
The result of payload..relatedParty[0]
is the value of key relatedParty
:
[
{
"id": "001",
"Role": "Account"
},
{
"id": "006",
"Role": "OpportunityId"
}
]
Now we apply [?($.Role=='Account')]
to that output. [? boolean_expression]
is a filter selector. Its documentation says that it returns:
Array or object containing key-value pairs if the DataWeave expression returns true. Otherwise, returns the value null.
It is not clear from the documentation but it takes an array as an input and applies the condition to each element of the array. Only elements that comply with the condition are added to the output.
$
here is the implicit variable for the lambda expression when you do not assign an explicit name for it. If you are not familiar with it see this answer: What are the uses of $ in dataweave?
Output of payload..relatedParty[0][?($.Role=='Account')]
:
[
{
"id": "001",
"Role": "Account"
}
]
Personally I prefer to use the filter() function which is more explicit:
payload..relatedParty[0] filter ($.Role=='Account')
In summary the sub expression that you are asking for is simply filtering elements under relatedParty
that have Role
equal to "Account".
Unrelated to the question, after that there is a nuance of DataWeave. The subexpression .id[0]
will work, but it could be confusing. .id
applied to an array will return an array of the values of the id
key. Then applying [0]
will return the first value in the list. It doesn't make a difference for this example, but for a large array it would make more sense to return the first value, then extract the value of id
:
payload..relatedParty[0][?($.Role=='Account')][0].id