如何在 dataweave 2 中将大 Json 响应分解为多个 Json?

How to break big Json response into multiple Json in dataweave 2?

我有 json 个响应,我需要将其分成多个部分才能发送到目标系统。

我正在尝试使用 for-each 但无法达到我想要的输出。

输入 json 看起来像这样

{
    parent :
    {
        child:
            [{
                a:[{},{},{},{}]
            }]  
    }
}

输出应该是这样的 第 1 部分

{
    parent :
    {
        child:
            [{
                a:[{},{}]
            }]  
    }
}

第二部分

{
    parent :
    {
        child:
            [{
                a:[{},{}]
            }]  
    }
}

有人可以帮我解决这个问题吗?

根据您的示例 json 很难判断,因为它未格式化 json。

注意 预期的 input/output 与我原来的答案有所不同,因此这是一个更新

但是,这有效并且可以根据您的实际输入进行调整。

首先,使用 foreach 迭代有效负载的集合部分(根据您的示例,它是 #[flatten(payload.parent.child.a)](但这可能会改变,因为不清楚 'child' 数组是否也超过一个元素。)

你也没有提到你想如何拆分它。根据您的示例,它适用于每 2 条记录。所以使用 batchSize 属性并设置为 2(将其更改为您的实际要求)。

然后您需要添加包装 json 元素,因为您将在 foreach 中丢失它:

  <foreach doc:name="For Each"  collection="#[flatten(payload.parent.child.a)]" batchSize="2">
            <ee:transform doc:name="Transform Message" doc:id="7b1cccb7-fcbb-41b3-a08f-bac5600df2f2" >
                <ee:message >
                    <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
    parent :
    {
        child:[
           a: payload
        ]
    }
}]]></ee:set-payload>
                </ee:message>
            </ee:transform>
            <logger level="ERROR" message="Split items here: #[payload]" />
        </foreach>

输出符合您的新预期输出:

  Split items here: {
      "parent": {
    "child": [
      {
        "a": [
          {

          },
          {

          }
        ]
      }
    ]
  }
}

Split items here: {
  "parent": {
"child": [
  {
    "a": [
      {

      },
      {

      }
    ]
  }
]

} }