我有一个如下所示的JSON
{
"packages": [
{
"screenings": [
{
"data": {
"educationHistory": [
{
"school": "Test education 4"
},
{
"school": "Test education 4"
}
]
}
},
{
"data": {
"educationHistory": [
{
"major": [
"Business Admin."
],
"school": "Test education 1"
},
{
"major": [
"Business Admin."
],
"school": "Test education 1"
}
]
}
}
]
}
]
}
在dataweave中,我这样写
ns0#EmploymentHistory: {(
payload.packages[0].screenings.data.educationHistory map {
ns0#Employer: {
ns0#EmployerName: $.school
}
}
)}
我正在得到这样的输出
<ns0:EmploymentHistory>
<ns0:Employer>
<ns0:EmployerName>
<school>Test education 4</school>
<school>Test education 4</school>
</ns0:EmployerName>
</ns0:Employer>
<ns0:Employer>
<ns0:EmployerName>
<school>Test education 1</school>
<school>Test education 1</school>
</ns0:EmployerName>
</ns0:Employer>
</ns0:EmploymentHistory>
我需要像下面的
<ns0:EmploymentHistory>
<ns0:Employer>
<ns0:EmployerName>
<school>Test education 4</school>
</ns0:EmployerName>
</ns0:Employer>
<ns0:Employer>
<ns0:EmployerName>
<school>Test education 4</school>
</ns0:EmployerName>
</ns0:Employer>
<ns0:Employer>
<ns0:EmployerName>
<school>Test education 1</school>
</ns0:EmployerName>
</ns0:Employer>
<ns0:Employer>
<ns0:EmployerName>
<school>Test education 1</school>
</ns0:EmployerName>
</ns0:Employer>
</ns0:EmploymentHistory>
此xpath payload.packages[0].screenings.data.educationHistory map {
遍历screening
而不是educationHistory
答案 0 :(得分:0)
您需要使用后代选择器..
来一次获取所有educationHistory
的值。这将从父元素(在本例中为payload.packages[0]
)中搜索整个树,并获取每个匹配的子元素(在本例中为educationHistory
)。由于educationHistory
在两个实例中都是一个数组,因此将返回一个数组数组,因此我们需要使用flatten
来代替,而是使用对象数组:
%dw 1.0
%output application/xml
%var employers = flatten payload.packages[0]..educationHistory
---
{
EmploymentHistory: {
(employers map {
Employer: {
EmployerName: {
School: $.school
}
}
})
}
}
您可以找到有关后代选择器here的信息。这是MuleSoft的官方说法:
[后代选择器]以
..
的形式应用于上下文,并在当前上下文下检索子树中所有匹配的key:value对的值。无论这些字段的组织层次结构如何,它们都将放置在输出中的同一级别。