在 Dataweave 中,选择器 .. 和 .* 有什么区别?
In Dataweave what is the difference between the selector .. and .*?
我看到文档说
Multi-value .*keyName Array of values of any matching keys
Descendants ..keyName Array of values of any matching descendant keys
但是我还是不明白其中的区别
我觉得很容易喜欢用例子来解释,所以根据this one你会得到下一个结果:
payload.breakfast_menu.food -> First food element
payload.breakfast_menu.*food -> List of food elements
payload.breakfast_menu.*name -> Nothing
payload.breakfast_menu..name -> List of all product name values
后代 returns 对象的每个嵌套级别中第一次出现的键的数组。
多值 returns 对象的当前嵌套级别中出现的所有键的数组。
如果你有这样的输入:
{
"id": 1,
"id": 11,
"secondLevel": {
"id": 2,
"id": 22,
"thirdLevel": {
"id": 3,
"id": 33
}
}
}
还有这个脚本:
%dw 2.0
output application/json
---
{
"descendant": payload..id, //first occurrence of "id" in each level
"multivalue": payload.*id, //all occurrence of "id" in the current level (the first level)
"multivalueSecondLevel": payload.secondLevel.*id, //all occurrence of "id" in the current level (the second level)
"allTheIds" : payload..*id //all the ID (descendant with multivalue)
}
它将生成此输出:
{
"descendant": [
1,
2,
3
],
"multivalue": [
1,
11
],
"multivalueSecondLevel": [
2,
22
],
"allTheIds": [
1,
11,
2,
22,
3,
33
]
}
更多详情见https://docs.mulesoft.com/mule-runtime/4.2/dataweave-cookbook-extract-data#descendants
我看到文档说
Multi-value .*keyName Array of values of any matching keys
Descendants ..keyName Array of values of any matching descendant keys
但是我还是不明白其中的区别
我觉得很容易喜欢用例子来解释,所以根据this one你会得到下一个结果:
payload.breakfast_menu.food -> First food element
payload.breakfast_menu.*food -> List of food elements
payload.breakfast_menu.*name -> Nothing
payload.breakfast_menu..name -> List of all product name values
后代 returns 对象的每个嵌套级别中第一次出现的键的数组。 多值 returns 对象的当前嵌套级别中出现的所有键的数组。
如果你有这样的输入:
{
"id": 1,
"id": 11,
"secondLevel": {
"id": 2,
"id": 22,
"thirdLevel": {
"id": 3,
"id": 33
}
}
}
还有这个脚本:
%dw 2.0
output application/json
---
{
"descendant": payload..id, //first occurrence of "id" in each level
"multivalue": payload.*id, //all occurrence of "id" in the current level (the first level)
"multivalueSecondLevel": payload.secondLevel.*id, //all occurrence of "id" in the current level (the second level)
"allTheIds" : payload..*id //all the ID (descendant with multivalue)
}
它将生成此输出:
{
"descendant": [
1,
2,
3
],
"multivalue": [
1,
11
],
"multivalueSecondLevel": [
2,
22
],
"allTheIds": [
1,
11,
2,
22,
3,
33
]
}
更多详情见https://docs.mulesoft.com/mule-runtime/4.2/dataweave-cookbook-extract-data#descendants