我有一个问题,在任何其他语言中我都需要十秒钟,但在这里我无法用Dataweave解决它。
我的JSON看起来像这样:
{
"100": [{
"courseID": "100",
"course": "physics",
"instructor": "John S",
"section": "A",
"date": "01-01-1900"
}],
"200": [{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"section": "A",
"date": "01-03-1900"
},
{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"section": "B",
"date": "01-07-1900"
},
{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"section": "C",
"date": "01-08-1900"
}
]}
}
我确定数组中的对象包含重复的信息,但截面和日期除外。所以我需要压缩它并将其转换成:
{
"courses": [{
"courseID": "100",
"course": "physics",
"instructor": "John S",
"sections": [{
"section": "A",
"date": "01-01-1900"
}]
},
{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"sections": [{
"section": "A",
"date": "01-03-1900"
},
{
"section": "B",
"date": "01-07-1900"
},
{
"section": "C",
"date": "01-08-1900"
}]
]}
我无法做到这一点。我已经尝试了使用map(),mapObject()和pluck()迭代文档的各种方法。它们需要嵌套我想,我只想从它迭代的第一个过程中提取ID,课程和讲师,然后继续使用section数据构建对象。嵌套似乎会导致问题,因为存在混合的对象和数组,如果嵌套函数返回错误的类型,则map()和mapObject()会抱怨。这是我一直没有成功的尝试:
%dw 2.0
output application/json
---
"courses": payload.pluck(courseValue, courseKey, courseIndex) ->
courseValue map (classValue, classKey, classIndex) ->
{ courseID: classValue.courseID,
instructor: classValue.instructor
etc...
}
我希望一切都清楚。我很长时间没有弄乱它了。是否有可能我想做的事情太复杂而DW无法处理,我应该使用另一种方法吗?
答案 0 :(得分:0)
此解决方案应适用于所有DataWeave版本
courses: (payload pluck $$) map using (c = payload[$]) {
courseID : $,
course: c[0].course,
instructor: c[0].instructor,
sections: c map {
section: $.section,
date: $.date
}
}
此解决方案适用于Mule 3.x但不适用于4.x,本博客文章在“从对象到数组的自动强制”下解释了这些解决方案':https://blogs.mulesoft.com/dev/mule-dev/dataweave-mule-4-beta-syntax-changes-part-1/
courses: payload map {
courseID: $[0].courseID,
course: $[0].course,
instructor: $[0].instructor,
sections: $ map {
section: $.section,
date: $.date
}
}
答案 1 :(得分:0)
我看到您正在使用DataWeave 2.0。这是您可以使用的简单DataWeave转换器。
<强>变压器:强>
<ee:transform doc:name="Transform Message" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
courses: (payload pluck $$) map ((courseValue, courseKey) -> {
courseID: payload[courseValue][0].courseID,
course: payload[courseValue][0].course,
instructor: payload[courseValue][0].instructor,
sections: payload[courseValue] map ((sectionValue, sectionKey) -> {
section: sectionValue.section,
date: sectionValue.date
})
})
}
]]></ee:set-payload>
</ee:message>
</ee:transform>
<强>输入:强>
{
"100": [
{
"courseID": "100",
"course": "physics",
"instructor": "John S",
"section": "A",
"date": "01-01-1900"
}
],
"200": [
{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"section": "A",
"date": "01-03-1900"
},
{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"section": "B",
"date": "01-07-1900"
},
{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"section": "C",
"date": "01-08-1900"
}
]
}
<强>输出:强>
{
"courses": [
{
"courseID": "100",
"course": "physics",
"instructor": "John S",
"sections": [
{
"section": "A",
"date": "01-01-1900"
}
]
},
{
"courseID": "200",
"course": "math",
"instructor": "Jim R",
"sections": [
{
"section": "A",
"date": "01-03-1900"
},
{
"section": "B",
"date": "01-07-1900"
},
{
"section": "C",
"date": "01-08-1900"
}
]
}
]
}