【问题标题】:Map multiple string Arrays using Dataweave into a single Array使用 Dataweave 将多个字符串数组映射到单个数组中
【发布时间】:2021-02-02 15:17:13
【问题描述】:

我有一个带有单个索引的数组,该数组有一个 JSON 对象,其中包含三个不同的字符串数组,我必须根据每个索引将它们映射到一个单独的数组中。例如,每个数组中的所有第一个索引到一个单独的 JSON 对象等等......

是否可以在 Dataweave 转换中实现?

输入

[{
    "id": [
          "123",
          "456",
          "789"
        ],
    "name": [
          "Test Brand 1",
          "Test Brand 2",
          "Test Brand 3"
        ],
    "address": [
          "Via Roma 1",
          "Via Milano 1",
          "Via Napoli 1"
        ]
}]

期望的输出:

[
    {
        "Key1": "123",
        "Key2": "Test Brand 1",
        "Key3": "Via Roma 1"
    },
    {
        "Key1": "456",
        "Key2": "Test Brand 2",
        "Key3": "Via Milano 1"
    },
    {
        "Key1": "789",
        "Key2": "Test Brand 3",
        "Key3": "Via Napoli 1"
    }
]

【问题讨论】:

    标签: arrays json dataweave anypoint-studio mulesoft


    【解决方案1】:

    您可以尝试以下 DataWeave 表达式,假设每个数组将始终包含相同数量的项目:

    %dw 2.0
    output application/json
    ---
    flatten(payload map (item, index1) -> 
        item.id map (item2, index2) -> {
            "Key1": item2,
            "Key2": item.name[index2],
            "Key3": item.address[index2]
        })
    

    输出(地址名称已更改以确保转换按预期工作):

    [
      {
        "Key1": "123",
        "Key2": "Test Brand 1",
        "Key3": "Via Roma 1"
      },
      {
        "Key1": "456",
        "Key2": "Test Brand 2",
        "Key3": "Via Milano 2"
      },
      {
        "Key1": "789",
        "Key2": "Test Brand 3",
        "Key3": "Via Napoli 3"
      }
    ]
    

    【讨论】:

    • 数组确实包含相同数量的值,抱歉没有指定它。这行得通,非常感谢:)
    • 很高兴它成功了!请不要忘记将此答案标记为已接受,因为它可以帮助可能遇到相同问题的其他人。谢谢!
    【解决方案2】:

    您可以尝试下面的代码,我们不需要进行两次迭代(循环),也不需要展平。

    %dw 2.0
    output application/json
    var id = payload[0].id
    ---
    id map (item2, index2) -> {
            "Key1": item2,
            "Key2": payload[0].name[index2],
            "Key3": payload[0].address[index2]
        }
    

    它会给出相同的结果。

    [
      {
        "Key1": "123",
        "Key2": "Test Brand 1",
        "Key3": "Via Roma 1"
      },
      {
        "Key1": "456",
        "Key2": "Test Brand 2",
        "Key3": "Via Milano 1"
      },
      {
        "Key1": "789",
        "Key2": "Test Brand 3",
        "Key3": "Via Napoli 1"
      }
    ]
    

    谢谢

    【讨论】:

      【解决方案3】:

      我对此进行了尝试,只是为了使其具有动态性,以便它适用于数组中的任意数量的键。由于您无法访问 reduce 中的索引,因此有点令人困惑。享受吧!

      %dw 2.0
      output application/json
      import indexOf from dw::core::Arrays
      var data = [{
          "id": [
                "123",
                "456",
                "789"
              ],
          "name": [
                "Test Brand 1",
                "Test Brand 2",
                "Test Brand 3"
              ],
          "address": [
                "Via Roma 1",
                "Via Milano 1",
                "Via Napoli 1"
              ]
      }]
      --- 
      (data flatMap valuesOf($)) map ((valuesArray) -> valuesArray reduce ((value, acc={}) -> acc ++ {
          ("key$(indexOf(valuesArray,value) + 1)"): value
      }))
      

      输出:

      [
        {
          "key1": "123",
          "key2": "456",
          "key3": "789"
        },
        {
          "key1": "Test Brand 1",
          "key2": "Test Brand 2",
          "key3": "Test Brand 3"
        },
        {
          "key1": "Via Roma 1",
          "key2": "Via Milano 1",
          "key3": "Via Napoli 1"
        }
      ]
      

      【讨论】:

        猜你喜欢
        • 2012-01-23
        • 2021-11-15
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 2017-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多