【问题标题】:Need Help in Dataweave在 Dataweave 中需要帮助
【发布时间】:2020-04-30 08:40:43
【问题描述】:

有人可以帮我将此数据编织转换为目标格式吗?基本上将项目转换为数组。

我什至开始了,但不知道如何进一步进行。任何建议都会有所帮助

  payload mapObject(order,key,index) -> {

         ((key):order) if ! (key  contains "Item_"),
    }

输入:

{
    "id": "1",
    "Item_1_ID": "43-583-0978",
    "Item_1_Name": "Sodium Chloride",
    "Item_1_Quantity": "26",
    "Item_1_Price": "802.41",
    "Item_2_ID": "71-788-5293",
    "Item_2_Name": "Ciprofloxacin",
    "Item_2_Quantity": "100",
    "Item_2_Price": "608.64",
   }

输出:

{
    "id": "1",
    Items:[{
        "ID":"43-583-0978",
        "Name": "Sodium Chloride",
        "Price": "802.41",
        "Quantity": "26",
    },
    {
        "ID":"71-788-5293",
        "Name": "Ciprofloxacin",
        "Price": "100",
        "Quantity": "608.64",
    }]

  }

【问题讨论】:

    标签: dataweave mulesoft


    【解决方案1】:

    这里有您所请求问题的解决方案。它使用一些正则表达式来提取键的各个部分,然后使用 groupBy 和 pluck 函数按 Id 编号分组,并使用 pluck 将 groupBy 结果转换为数组并转换键。

    %dw 2.0
    output application/json
    fun getItemNumber(keyName:String) = 
        (keyName scan /Item_(\d+)/)[0][1]
    
    fun getKeyName(keyName:String) = 
        (keyName scan /Item_[\d+]_(.*)/)[0][1]    
    ---
    {
        "Id" : payload.id,
        "Item": (payload - "id") 
                    groupBy ((value, key) -> //Group it by ID
                                getItemNumber(key as String)
                            ) 
                    pluck ((value, key, index) -> //Go through all the values of the groups and take the real keys
                            value mapObject ((value, key, index) -> {
                                (getKeyName(key)): value
                            })
                        )        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2020-11-16
      • 2014-06-27
      • 2021-05-14
      • 2013-07-17
      • 2013-01-31
      相关资源
      最近更新 更多