[英]Dataweave: How to concatenate values for a certain key in json array
[英]Dataweave: How to filter and concatenate values for certain keys in json array
我的輸入是
{
"value": [
{
"FirstName": "LISA",
"Sku1": 10074895,
"Sku2": 10074896,
"Sku3": 10074897
},
{
"FirstName": "JOSE",
"Sku1": 10128582,
"Sku2": null,
"Sku3": null
}
]
}
我想要的 Output 是一個連接所有 SKU 的字符串
{
"value": [
{
"FirstName": "LISA",
"Skus": "10074895, 10074896, 10074897"
},
{
"FirstName": "JOSE",
"Skus": "10128582"
}
]
}
我是 Dataweave 的新手,不知道該怎么做 提前謝謝
我的輸入將有比“名字”更多的鍵,我想排除這些鍵
{
"value": [
{
"FirstName": "LISA",
"Country": "CA",
"Sku1": 10074895,
"Sku2": 10074896,
"Sku3": 10074897
},
{
"FirstName": "JOSE",
"Country": "CA",
"Sku1": 10128582,
"Sku2": null,
"Sku3": null
}
]
}
這是我想出的解決方案,有人可以幫助解決此解決方案。 問題是 output 中的“Skus”末尾有一個尾隨“,”
%dw 2.0
output application/json
fun mapToResponseModel(customObj) = do {
var skus = createSkus(customObj)
var withoutSkus = mapWithoutSkus(customObj)
---
withoutSkus ++ { "Skus": skus }
}
fun createSkus(customObj) =
keysOf(customObj) reduce (key, acc = "") ->
if ((key contains "Sku") and (customObj[key] != null))
customObj[key] ++ "," ++ acc
else
acc
fun mapWithoutSkus(customObj) =
customObj mapObject (value, key) ->
if (value != null and !(key contains "Sku"))
{ (key): value }
else
{}
---
{
value: payload.value map (customObj) ->
mapToResponseModel(customObj)
}
Output
{
"value": [
{
"FirstName": "LISA",
"Country": "CA",
"Skus": "10074897,10074896,10074895,"
},
{
"FirstName": "JOSE",
"Country": "CA",
"Skus": "10128582,"
}
]
}
試試這種方法:
在結果對象中,就 Skus 而言,我們獲取迭代對象並從中刪除FirstName
,這給我們留下了 Sku1、Sku2、Sku3。 過濾相同的內容以避免空值,然后使用pluck獲取數組中的值,之后您可以使用joinBy來形成字符串。
腳本
%dw 2.0
output application/json
---
value: payload.value map {
"FirstName": $.FirstName,
"Skus": (($ - "FirstName") filterObject($ != null) pluck $ joinBy ",")
}
輸出
{
"value": [
{
"FirstName": "LISA",
"Skus": "10074895,10074896,10074897"
},
{
"FirstName": "JOSE",
"Skus": "10128582"
}
]
}
對於編輯 1,嘗試使用以下腳本:
%dw 2.0
output application/json
var skuValues = payload.value map {
($ filterObject (($$) contains "Sku" ) mapObject {
a : $
})
}
---
value: payload.value map {
"FirstName": $.FirstName,
"Country" : $.Country,
"Skus": skuValues [($$)] filterObject($ != null) pluck $ joinBy ","
}
不確定我是否應該這樣做,盡管您可以修改您的函數以檢查正在使用的 sku* 鍵是否是最后一個鍵(不要在末尾添加“,”)。
fun createSkus(customObj) =
keysOf(customObj) reduce (key, acc = "") ->
if ((key contains "Sku") and (customObj[key] != null) and (keysOf(customObj)[-1] != key ))
acc ++ customObj[key] ++ ","
else if ((key contains "Sku") and (customObj[key] != null) and (keysOf(customObj)[-1] == key ))
acc ++ customObj[key]
else
acc
寫了這么多函數,難道只是為了模塊化? 下面可以在沒有外部功能的情況下嘗試:
%dw 2.0
output application/json
---
value : (payload.value map ($ mapObject ((value, key, index) ->
((key) : value ) if !(key contains "Sku")
)
++
"Skus" : joinBy(valuesOf($ filterObject ((skuVal, skuKey, indexOfSku) -> (skuKey contains "Sku") and !isBlank(skuVal))),",")
))
使用您的函數,需要進行一些修改,如下所示,以便在末尾沒有額外的逗號:
%dw 2.0
output application/json
fun mapToResponseModel(customObj) = do {
var skus = joinBy(createSkus(customObj),",")
var withoutSkus = mapWithoutSkus(customObj)
---
withoutSkus ++ { "Skus": skus }
}
fun createSkus(customObj) =
keysOf(customObj) reduce (key, acc = []) ->
if ((key contains "Sku") and (customObj[key] != null))
acc << customObj[key]
else
acc
fun mapWithoutSkus(customObj) =
customObj mapObject (value, key) ->
if (value != null and !(key contains "Sku"))
{ (key): value }
else
{}
---
{
value: payload.value map (customObj) ->
mapToResponseModel(customObj)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.