【问题标题】:Mule 4 dataweave 2.0 mapping nested array logicMule 4 dataweave 2.0 映射嵌套数组逻辑
【发布时间】:2019-11-23 22:31:19
【问题描述】:

我低于 JSON 请求

[{
"Weight": "787.00",
"Volume": "65.00",
"TrackID": "128260490",
"item": [
  {
    "Description": "basketball",
    "totalquantity": 1
  },
  {
    "Description": "football",
    "totalquantity": 4
  }
  ]
 },
 {
"Weight": "68.200",
"Volume": "44.298",
"TrackID": "890433466",
"item": [
  {
    "Description": "hockeystick",
    "totalquantity": 8
  }
  ]
 }
]

我正在以以下方式查看输出(仅限 json 格式):

    { 
     Purchasedetails: [
     {
     "TrackID": 128260490,
    "Description": "basketball",
     "totalquantity": 1
     },
    {
     "TrackID": 128260490,
     "Description": "football",
    "totalquantity": 4
     },
    {
    "TrackID": 890433466,
    "Description": "hockeystick",
    "totalquantity": 8
    }
   ]
}

在这里,如果您看到篮球和足球(描述)中的跟踪 ID 非常相似,因为它们具有共同的 TrackID,我该如何处理这个 TrackID 逻辑?

【问题讨论】:

    标签: arrays mapping dataweave mulesoft mule4


    【解决方案1】:

    试试这个:

    %dw 2.0
    output application/json
    ---
    // Create the object with the PurchaseDetails field
    {
        // Iterate over every single object in the array
        // NOTE: I prefare reduce because it saves iterations and/or array removals
        PurchaseDetails: payload reduce (  
            (pd, result=[]) -> (
                // Append the array of items with the TrackID to the result
                result ++ (
                    // Build a new object for each object in the item array
                    pd.item map { 
                        // Add the TrackID to the resulting object
                        TrackID: pd.TrackID,
                        ($)
                    }
                )
            )
        )
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2019-08-13
      相关资源
      最近更新 更多