You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DataWeave将含可选子元素的JSON多维数组转一维数组

Solution to Flatten JSON Array with Parent-Child Attribute Merging in DataWeave

Got it, let's tackle this problem step by step. You need to flatten a nested JSON array where parent attributes repeat for each child, and handle cases where some parents don't have children. Plus, avoiding hardcoding every attribute name makes total sense for hundreds of fields. Here's how to do it with DataWeave 2.x:


Scenario 1: Keep All Attributes

First, let's handle full attribute flattening. The key here is using flatMap to iterate over each parent, then either expanding its children into individual entries (merging parent and child attributes) or returning the parent itself if there are no children. We'll use merge to combine objects automatically—no need to list every attribute!

Input Payload

[
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "child": [
      { "childAttribute1": "inner1-1-1", "childAttribute2": "inner1-1-2" },
      { "childAttribute1": "inner1-2-1", "childAttribute2": "inner1-2-2" },
      { "childAttribute1": "inner1-3-1", "childAttribute2": "inner1-3-2" }
    ]
  },
  {
    "id": "2",
    "parentAttribute1": "parent2-1",
    "parentAttribute2": "parent2-2",
    "parentAttribute3": "parent2-3",
    "child": [ { "childAttribute1": "inner2-1-1", "childAttribute2": "inner2-1-2" } ]
  },
  {
    "id": "3",
    "parentAttribute1": "parent3-1",
    "parentAttribute2": "parent3-2",
    "parentAttribute3": "parent3-3"
  }
]

DataWeave Script

%dw 2.0
output application/json
---
payload flatMap (parent) -> 
  if (parent.child != null and sizeOf(parent.child) > 0) 
    parent.child map (child) -> (parent - "child") merge child
  else 
    [parent]

How It Works

  • flatMap processes each parent object: if it has children, it expands them into separate entries; if not, it returns the parent as a single entry.
  • parent - "child" removes the child array from the parent so we don't include it in the flattened result.
  • merge automatically combines the parent's attributes (minus the child array) with each child's attributes—no manual field mapping required.
  • The if condition handles parents without a child array gracefully.

Output (Matches Scenario 1)

[
  { "id": "1", "parentAttribute1": "parent1-1", "parentAttribute2": "parent1-2", "parentAttribute3": "parent1-3", "childAttribute1": "inner1-1-1", "childAttribute2": "inner1-1-2" },
  { "id": "1", "parentAttribute1": "parent1-1", "parentAttribute2": "parent1-2", "parentAttribute3": "parent1-3", "childAttribute1": "inner1-2-1", "childAttribute2": "inner1-2-2" },
  { "id": "1", "parentAttribute1": "parent1-1", "parentAttribute2": "parent1-2", "parentAttribute3": "parent1-3", "childAttribute1": "inner1-3-1", "childAttribute2": "inner1-3-2" },
  { "id": "2", "parentAttribute1": "parent2-1", "parentAttribute2": "parent2-2", "parentAttribute3": "parent2-3", "childAttribute1": "inner2-1-1", "childAttribute2": "inner2-1-2" },
  { "id": "3", "parentAttribute1": "parent3-1", "parentAttribute2": "parent3-2", "parentAttribute3": "parent3-3" }
]

Scenario 2: Keep Only Selected Attributes

For this case, we can define lists of parent and child attributes to retain, then filter both objects before merging. This keeps the script scalable even if you need to adjust the selected fields later.

DataWeave Script

%dw 2.0
output application/json
var parentAttributesToKeep = ["id", "parentAttribute1"]
var childAttributesToKeep = ["childAttribute1"]
---
payload flatMap (parent) -> 
  if (parent.child != null and sizeOf(parent.child) > 0) 
    parent.child map (child) -> 
      (parent filterObject ((value, key) -> parentAttributesToKeep contains key)) 
      merge 
      (child filterObject ((value, key) -> childAttributesToKeep contains key))
  else 
    [parent filterObject ((value, key) -> parentAttributesToKeep contains key)]

How It Works

  • We define parentAttributesToKeep and childAttributesToKeep as arrays of the attribute names we want to retain.
  • filterObject narrows down both parent and child objects to only the specified fields before merging.
  • The core flatMap logic remains the same as Scenario 1, ensuring consistent handling of parents with or without children.

Output (Matches Scenario 2)

[
  { "id": "1", "parentAttribute1": "parent1-1", "childAttribute1": "inner1-1-1" },
  { "id": "1", "parentAttribute1": "parent1-1", "childAttribute1": "inner1-2-1" },
  { "id": "1", "parentAttribute1": "parent1-1", "childAttribute1": "inner1-3-1" },
  { "id": "2", "parentAttribute1": "parent2-1", "childAttribute1": "inner2-1-1" },
  { "id": "3", "parentAttribute1": "parent3-1" }
]

This approach scales easily for hundreds of attributes—just add the names to the respective arrays in Scenario 2, or use the first script for full attribute retention. No manual field mapping needed!

内容的提问来源于stack exchange,提问作者kimmy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:53:31