为什么Mule DataWeave阵列贴图会剥离顶级对象? [英] Why does Mule DataWeave array map strip top level objects?

查看:80
本文介绍了为什么Mule DataWeave阵列贴图会剥离顶级对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解DataWeave v1.0在映射根JSON数组中的对象时的行为.

I'm trying to understand the behaviour of DataWeave v1.0 when it comes to mapping objects in a root JSON array.

在此阶段,我只想按原样映射数组中的每个项目,而不映射该项目的每个单独字段.我需要对数组中的每个项目都执行此操作,因为稍后我要编辑 some 个字段,但是由于可能存在许多字段,我不希望单独映射它们的开销,一个.

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.

这是我的数据结构:

%dw 1.0
%output application/json
---
payload map {
    ($)
}

这是我的输入:

[
  {
    "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"
    }
  }
]

您可以看到对象AnObject丢失了,尽管它的子对象仍然存在.

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"
        }
    ]
  }
]

引发错误:

Cannot coerce a :array to a :object.

我也对根数组项进行了mapObject操作,但是我总是遇到相同的行为.有谁能解释这里发生的事情,并告诉我如何动态地复制根有效负载中的每个项目.

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.

M子运行时为3.9.1.

Mule runtime is 3.9.1.

推荐答案

要遍历数组中的每个项目并按原样进行操作,您应该执行payload map $,与payload map ((item) -> item)

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 ((item) -> {(item)})相同.

这里为每一项返回的是表达式{(expr)},它在运行于Mule 3.9.1的DW版本中具有偶然的行为,该表达式试图强制expr(在这种情况下为一个对象)转换为对象数组,然后它将尝试将父对象内该强制数组中的所有对象展平.似乎也在尝试强制更改键的值,这就是DW引发错误的原因.

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. 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.

这篇关于为什么Mule DataWeave阵列贴图会剥离顶级对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆