Mule 4 Dataweave:从脚本输出到 JSON 的数组

发布于 2025-01-09 08:56:12 字数 542 浏览 0 评论 0原文

执行脚本模块后,我得到以下输出:

[
"0": [
"0": ["A","B","C"],
"1": ["D","E","F"]
],
"1": [
"0": ["A","B","C"],
"1": ["D","E","F"],
"2": ["G","H","I"]
],
"2": [
"0": ["A","B","C"],
"1": ["D","E","F"],
"2": ["G","H","I"]
]
]

我只想获取值,而不是键。所以预期的输出是:

[
   [
      ["A","B","C"],
      ["D","E","F"]
   ],
   [
      ["A","B","C"],
      ["D","E","F"],
      ["G","H","I"]
   ],
   [
      ["A","B","C"],
      ["D","E","F"],
      ["G","H","I"]
   ]
]

我尝试使用map函数和valuesOf函数,但没有任何运气。

谢谢!

After a script module execution, I get the output below:

[
"0": [
"0": ["A","B","C"],
"1": ["D","E","F"]
],
"1": [
"0": ["A","B","C"],
"1": ["D","E","F"],
"2": ["G","H","I"]
],
"2": [
"0": ["A","B","C"],
"1": ["D","E","F"],
"2": ["G","H","I"]
]
]

And I would like to get only the values, not the keys. So the expected output would be:

[
   [
      ["A","B","C"],
      ["D","E","F"]
   ],
   [
      ["A","B","C"],
      ["D","E","F"],
      ["G","H","I"]
   ],
   [
      ["A","B","C"],
      ["D","E","F"],
      ["G","H","I"]
   ]
]

I tried to use the map function and the valuesOf function, but without any luck.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2025-01-16 08:56:12

假设输出看起来像这样的另一个选项,

[
  {
    "0": {
      "0": ["A","B","C"],
      "1": ["D","E","F"]
    }
  },
  {
    "1": {
      "0": ["A","B","C"],
      "1": ["D","E","F"],
      "2": ["G","H","I"]
    }
  },
  {
    "2": {
      "0": ["A","B","C"],
      "1": ["D","E","F"],
      "2": ["G","H","I"]
    }
  }
]

此脚本将其转换为您想要的

payload map (valuesOf($) flatMap valuesOf($))

输出

[
  [
    ["A","B","C"],
    ["D","E","F"]
  ],
  [
    ["A","B","C"],
    ["D","E","F"],
    ["G","H","I"]
  ],
  [
    ["A","B","C"],
    ["D","E","F"],
    ["G","H","I"]
  ]
]

Another option supposing the output looks like this

[
  {
    "0": {
      "0": ["A","B","C"],
      "1": ["D","E","F"]
    }
  },
  {
    "1": {
      "0": ["A","B","C"],
      "1": ["D","E","F"],
      "2": ["G","H","I"]
    }
  },
  {
    "2": {
      "0": ["A","B","C"],
      "1": ["D","E","F"],
      "2": ["G","H","I"]
    }
  }
]

this script turns it into your desired output

payload map (valuesOf($) flatMap valuesOf($))

output

[
  [
    ["A","B","C"],
    ["D","E","F"]
  ],
  [
    ["A","B","C"],
    ["D","E","F"],
    ["G","H","I"]
  ],
  [
    ["A","B","C"],
    ["D","E","F"],
    ["G","H","I"]
  ]
]
病女 2025-01-16 08:56:12

假设输入和第一个子级确实是对象,您可以在顶层使用 pluck() 来提取对象数组,然后映射每个子级以再次使用 pluck() 。

输入:

{
    "0": {
        "0": ["A","B","C"],
        "1": ["D","E","F"]
    },
    "1": {
        "0": ["A","B","C"],
        "1": ["D","E","F"],
        "2": ["G","H","I"]
    },
    "2": {
        "0": ["A","B","C"],
        "1": ["D","E","F"],
        "2": ["G","H","I"]
    }
}

脚本:

%dw 2.0
output application/json  
---
payload 
    pluck $ 
    flatMap 
        ($ pluck ((value, key, index) -> value))

输出:

[
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ]
  ],
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ],
    [
      "G",
      "H",
      "I"
    ]
  ],
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ],
    [
      "G",
      "H",
      "I"
    ]
  ]
]

Assuming the input and the first childs are really objects, you can use pluck() at the top level to extract as an array of objects, then map each of the childs to use pluck() again.

Input:

{
    "0": {
        "0": ["A","B","C"],
        "1": ["D","E","F"]
    },
    "1": {
        "0": ["A","B","C"],
        "1": ["D","E","F"],
        "2": ["G","H","I"]
    },
    "2": {
        "0": ["A","B","C"],
        "1": ["D","E","F"],
        "2": ["G","H","I"]
    }
}

Script:

%dw 2.0
output application/json  
---
payload 
    pluck $ 
    flatMap 
        ($ pluck ((value, key, index) -> value))

Output:

[
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ]
  ],
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ],
    [
      "G",
      "H",
      "I"
    ]
  ],
  [
    [
      "A",
      "B",
      "C"
    ],
    [
      "D",
      "E",
      "F"
    ],
    [
      "G",
      "H",
      "I"
    ]
  ]
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文