简体   繁体   English

为什么Mule DataWeave阵列贴图会剥离顶级对象?

[英]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. 我试图了解DataWeave v1.0在映射根JSON数组中的对象时的行为。

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. 如您所见,对象AnObject丢失了,尽管它的子对象仍然存在。

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. 我也曾在根数组项上使用mapObject操作,但是我总是遇到相同的行为。 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. Mule运行时为3.9.1。

To go through each item in the array and let them as it is, you should do payload map $ , that is the same as payload map ((item) -> item) 要遍历数组中的每个项目并按原样进行操作,您应该执行payload map $ ,与payload map ((item) -> item)相同payload map ((item) -> item)

What you were doing is the same as: payload map ((item) -> {(item)}) . 您正在执行的操作与: payload map ((item) -> {(item)})

Here what you are returning for each item is the expression {(expr)} that in the DW version that runs on Mule 3.9.1, it has an accidental behavior where the expression tries to coerce expr (that in this case is an object) to array of objects and then it will try to flatten all the objects in that coerced array inside the parent object. 在这里,您为每一项返回的是表达式{(expr)} ,该表达式在运行于Mule 3.9.1的DW版本中具有偶然的行为,该表达式试图强制expr (在本例中为对象)到对象数组,然后它将尝试展平父对象内该强制数组中的所有对象。 It looks like is trying to coerce the value of the keys too, that's why DW throws the error. 似乎也试图强制更改键的值,这就是DW引发错误的原因。

This behavior of {()} changes in newer versions of DW. {()}此行为在较新版本的DW中发生了变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM