首页 > 解决方案 > 使用 dataweave 2.0 将 JSON 数组转换为对象

问题描述

我在下面有一个层次结构的有效负载,并试图根据列“DeleteIndicator”=“Y”进行过滤。这些是我将得到的固定列。我想遍历有效负载并检查 DeleteIndicator。更新了内部数组中多个子项的输入和预期输出。

输入:

    {
   "Num":4363886,
   "LineItems":[
      {
         "DetailGUID":"B439E023360C",
         "DeleteIndicator":"Y"
      },
      {
         "DetailGUID":"B439E023360C",
         "LineQuantity":[
            {
               "AltGUID":"2B43AC4203DC",
               "DeleteIndicator":"Y"
            },
            {
               "AltGUID":"2B43AC4203DD",
               "DeleteIndicator":"Y"
            }
         ]
      },
      {
         "DetailGUID":"B439E023360C",
         "LineQuantity":[
            {
               "AltGUID":"2B43AC4203DC",
               "ShipTo":[
                  {
                     "ShipToGUID":"2B43AC4201AB",
                     "DeleteIndicator":"Y"
                  },
                  {
                     "ShipToGUID":"2B43AC4201AC",
                     "DeleteIndicator":"Y"
                  }
               ]
            }
         ]
      }
   ]
}

预期输出:

{
   "Num":4363886,
   "Details":[
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":null,
         "ShipToGUID":null
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DC",
         "ShipToGUID":null
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DD",
         "ShipToGUID":null
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DC",
         "ShipToGUID":"2B43AC4201AB"
      },
      {
         "DetailGUID":"B439E023360C",
         "AltGUID":"2B43AC4203DC",
         "ShipToGUID":"2B43AC4201AC"
      }
   ]
}

标签: dataweavemule4

解决方案


好吧,这有点复杂,但我已尽力使代码可读并记录在案。尝试从不同的角度看待问题。认为您正试图在所有“LineQuantity”中“传播”“LineItems”的字段。

%dw 2.0

// This Function takes a json and an array and spreads the json fields
// across each elements of the array.
fun spreadAcrossArray(json: Object, array: Array | Null) = (array default [{}]) map {
    (json),
    ($)
}

//Here is the main logic of getting the payload.
//The idea is first to "Spread" the lineItem Elements across all the "LineQuantity" 
//and then spread those to all "ShipTo" elements. 
fun spreadLineItemAcrossLineQuantity(lineItems: Array) = lineItems map ((lineItem) -> do {
     var lineItemSpreadedAcrossQuantity = 
        {DetailGUID: lineItem.DetailGUID} spreadAcrossArray lineItem.LineQuantity
     ---
     flatten(lineItemSpreadedAcrossQuantity map ({
            DetailGUID: $.DetailGUID,
            AltGUID: $.AltGUID
        } spreadAcrossArray $.ShipTo)) //Map app the lineItemSpreadedAcrossQuantity with spreading them to lineItemSpreadedAcrossQuantity.ShipTo
    }
)

output application/json
---
{
    Num: payload.Num,
    Details: flatten(spreadLineItemAcrossLineQuantity(payload.LineItems))
    map {
        DetailGUID: $.DetailGUID,
        AltGUID: $.AltGUID,
        ShipToGUID: $.ShipToGUID
    }
} 

它需要稍后将数组展平,因为您基本上是在spreadAcrossArray函数中传递了一个对象,而作为回报,您将获得一个数组。而且 map 函数也返回一个数组,所以你得到一个数组数组。


推荐阅读