我有这样的JSON:
{
"invoice-line": [
{
"id": "01",
"date": "2019-06-21",
"sales-date": "2019-06-21",
"line-num": "1",
"item": "Mouse",
"descrption": "This is line 1 item"
},
{
"id": "02",
"date": "2019-06-21",
"sales-date": "2019-06-21",
"line-num": "2",
"item": "Keyboard",
"descrption": "This is line 2 item"
},
{
"id": "03",
"date": "2019-06-21",
"sales-date": "2019-06-21",
"line-num": "3",
"item": "Monitor",
"descrption": "This is line 3 item"
}
]
}
我们需要从此输入JSON中提取数据并映射到另一个输出JSON,但要具有逻辑:
当line-num = 1时,则为地图ID,日期,销售日期,描述
然后休息,否则数组将映射id,item,descrption
如下所示:
{
"row": [
{
"id": "01",
"date": "2019-06-21",
"sales-date": "2019-06-21",
"line-num": "1",
"item": "",
"descrption": "This is line 1 item"
},
{
"id": "02",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Mouse",
"descrption": "This is line 2 item"
},
{
"id": "03",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Monitor",
"descrption": "This is line 3 item"
}
]
}
还可以执行将数组项(第一个索引)映射到不同输出数组的操作吗?例如:
{
"row": [
{
"id": "",
"date": "2019-06-21",
"sales-date": "2019-06-21",
"line-num": "1",
"item": "",
"descrption": ""
},
{
"id": "01",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Keyboard",
"descrption": "This is line 1 item"
},
{
"id": "02",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Mouse",
"descrption": "This is line 2 item"
},
{
"id": "03",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Monitor",
"descrption": "This is line 3 item"
}
]
}
答案 0 :(得分:1)
希望这是您想要的:
%dw 1.0
%output application/json
---
row:
(payload.invoice-line filter ($.line-num == '1') map {
id: "",
date: $.date,
sales-date: $.sales-date,
line-num: $.line-num,
item: "",
descrption: ""
})
++
(payload.invoice-line map {
id: $.id,
date: "",
sales-date: "",
line-num: "",
item: $.item,
descrption: $.descrption
})
产生:
{
"row": [
{
"id": "",
"date": "2019-06-21",
"sales-date": "2019-06-21",
"line-num": "1",
"item": "",
"descrption": ""
},
{
"id": "01",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Mouse",
"descrption": "This is line 1 item"
},
{
"id": "02",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Keyboard",
"descrption": "This is line 2 item"
},
{
"id": "03",
"date": "",
"sales-date": "",
"line-num": "",
"item": "Monitor",
"descrption": "This is line 3 item"
}
]
}