mule - Why does Mule DataWeave array map strip top level objects?
问题描述
I'm trying to understand the behaviour of DataWeave v1.0 when it comes to mapping objects in a root JSON array.
At this stage I just want to map each item in the array as-is without mapping each individual field of the item. I need to do it for each item in the array because later on I want to edit some of the fields, but since there are potentially many I don't want the overhead of mapping them one-by-one.
This is my dataweave:
%dw 1.0
%output application/json
---
payload map {
($)
}
This is my input:
[
{
"MyString": "ABCD",
"MyNumber": 123,
"AnObject": {
"MyBool": false,
"MyNestedObject": {
"MyNestedString": "DEF"
}
}
}
]
I want my output to be (at this stage) exactly the same as my input.
Instead my (wrong) output is:
[
{
"MyString": "ABCD",
"MyNumber": 123,
"MyBool": false,
"MyNestedObject": {
"MyNestedString": "DEF"
}
}
]
As you can see the object AnObject
is missing, although its children remain.
Things are worse if the input includes arrays, for example the input:
[
{
"MyString": "ABCD",
"MyNumber": 123,
"AnObject": {
"MyBool": false,
"MyNestedObject": {
"MyNestedString": "DEF"
}
},
"AnArray": [
{
"Title": "An array item",
"Description": "Pretty standard"
}
]
}
]
Throws the error:
Cannot coerce a :array to a :object.
I have played around with the mapObject
operation on the root array items too, but I always run into the same behaviour. Is anyone able to explain what is happening here, and show me how I can copy each item in the root payload across dynamically.
Mule runtime is 3.9.1.
解决方案
要遍历数组中的每个项目并让它们保持原样,您应该这样做payload map $
,这与payload map ((item) -> item)
您所做的与以下内容相同:payload map ((item) -> {(item)})
.
在这里,您为每个项目返回的是{(expr)}
在 Mule 3.9.1 上运行的 DW 版本中的表达式,它有一个意外行为,表达式试图强制expr
(在这种情况下是一个对象)到对象数组,然后它将尝试展平父对象内该强制数组中的所有对象。看起来也试图强制键的值,这就是 DW 抛出错误的原因。
这种行为{()}
在较新版本的 DW 中有所改变。
推荐阅读
- c# - .NET Core 对外部服务的缓慢请求
- html - 如何让下拉菜单工作并将导航栏保持在顶部?
- java - 列表如何在 Java 中保存对象引用/值?
- javascript - 如果满足某些条件,则阻止呈现 JSON 对象
- html - Powershell通过类名从HTML中获取元素
- java - 如何让我的 java 编译器在 Ubuntu Linux 上识别旧版本的 OpenJFX for Java 8?
- sql - 用于创建表示历史的结果的 SQL 查询
- python - 声纳传感器发回随机数据并随机关闭
- javascript - ASP.Net Core JS 适用于 _Layout,但不适用于正文页面
- machine-learning - 机器学习模型可以用于预测 TRNG 输出吗?