使用 DataWeave 2.0 将有效载荷转换为自定义对象
Transformation of Payload to custom object using DataWeave 2.0
我有一个输入有效负载(json 数组)需要使用自定义格式进行丰富。
输入负载:
{
"report": [
{
"totPills": 100,
"food": "chm",
"drink": "kgf",
"202201": 1,
"202202": null,
"202203": 0.39
},
{
"totPills": 100,
"food": "mkm",
"drink": "wfl",
"202201": 0.0,
"202202": 1.36,
"202203": 0
}
]
}
在上面的负载属性中 202201
、202202
、202203
是动态属性。
预期输出:
{
"totPills": 100,
"report": [
{
"food": "chm",
"drink": "kgf",
"qty": {
"202201": 1,
"202202": null,
"202203": 0.39
}
},
{
"food": "mkm",
"drink": "wfl",
"qty": {
"202201": 0.0,
"202202": 1.36,
"202203": 0
}
}
]
}
我尝试按照脚本获得以上结果(它没有提供预期的结果):
%dw 2.0
import * from dw::core::Strings
output application/json
var dynWeek = "'202201','202202','202203'"
var pl = payload.report default[] map ((item, index) -> item - 'totPills' )
var pl1 = pl default [] map(($ default {}) mapObject ((value, key, index) -> {
((key):value) if ((dynWeek contains((key as String))) == false)
}) )
var pl2 = pl default [] map(($ default {}) mapObject ((value, key, index) -> {
((key):value) if ((dynWeek contains((key as String))))
}) )
---
{
"totPills": payload.report[0].totPills,
"report":pl1 ++ pl2 /* Not producing expected results */
}
我有几个问题:
- 如何动态实现预期输出?
- 这种方法有性能影响吗?
谢谢,
瑞克
我将 report
的映射分成两组 key-values,它们的键在 dynWeek 中,而那些不在。
%dw 2.0
output application/json
var dynWeek = ['202201','202202','202203']
fun getInList(obj: Object, list: Array, in: Boolean)= obj filterObject (
do {
var isIn = list contains ($$ as String)
---
if (in) isIn else !isIn
}
)
---
{
"totPills": payload.report[0].totPills,
"report": payload.report
map ({
(getInList($ - "totPills", dynWeek, false)), // fields not in dynWeek
qty: getInList($, dynWeek, true) // fields in dynWeek
})
}
输出:
{
"totPills": 100,
"report": [
{
"food": "chm",
"drink": "kgf",
"qty": {
"202201": 1,
"202202": null,
"202203": 0.39
}
},
{
"food": "mkm",
"drink": "wfl",
"qty": {
"202201": 0.0,
"202202": 1.36,
"202203": 0
}
}
]
}
这应该有效:
%dw 2.0
output application/json
---
{
"totPills": payload.report[0].totPills,
"report": payload.report map {
food: $.food,
drink: $.drink,
qty: ($ - "food" - "drink" - "totPills")
}
}
我有一个输入有效负载(json 数组)需要使用自定义格式进行丰富。
输入负载:
{
"report": [
{
"totPills": 100,
"food": "chm",
"drink": "kgf",
"202201": 1,
"202202": null,
"202203": 0.39
},
{
"totPills": 100,
"food": "mkm",
"drink": "wfl",
"202201": 0.0,
"202202": 1.36,
"202203": 0
}
]
}
在上面的负载属性中 202201
、202202
、202203
是动态属性。
预期输出:
{
"totPills": 100,
"report": [
{
"food": "chm",
"drink": "kgf",
"qty": {
"202201": 1,
"202202": null,
"202203": 0.39
}
},
{
"food": "mkm",
"drink": "wfl",
"qty": {
"202201": 0.0,
"202202": 1.36,
"202203": 0
}
}
]
}
我尝试按照脚本获得以上结果(它没有提供预期的结果):
%dw 2.0
import * from dw::core::Strings
output application/json
var dynWeek = "'202201','202202','202203'"
var pl = payload.report default[] map ((item, index) -> item - 'totPills' )
var pl1 = pl default [] map(($ default {}) mapObject ((value, key, index) -> {
((key):value) if ((dynWeek contains((key as String))) == false)
}) )
var pl2 = pl default [] map(($ default {}) mapObject ((value, key, index) -> {
((key):value) if ((dynWeek contains((key as String))))
}) )
---
{
"totPills": payload.report[0].totPills,
"report":pl1 ++ pl2 /* Not producing expected results */
}
我有几个问题:
- 如何动态实现预期输出?
- 这种方法有性能影响吗?
谢谢, 瑞克
我将 report
的映射分成两组 key-values,它们的键在 dynWeek 中,而那些不在。
%dw 2.0
output application/json
var dynWeek = ['202201','202202','202203']
fun getInList(obj: Object, list: Array, in: Boolean)= obj filterObject (
do {
var isIn = list contains ($$ as String)
---
if (in) isIn else !isIn
}
)
---
{
"totPills": payload.report[0].totPills,
"report": payload.report
map ({
(getInList($ - "totPills", dynWeek, false)), // fields not in dynWeek
qty: getInList($, dynWeek, true) // fields in dynWeek
})
}
输出:
{
"totPills": 100,
"report": [
{
"food": "chm",
"drink": "kgf",
"qty": {
"202201": 1,
"202202": null,
"202203": 0.39
}
},
{
"food": "mkm",
"drink": "wfl",
"qty": {
"202201": 0.0,
"202202": 1.36,
"202203": 0
}
}
]
}
这应该有效:
%dw 2.0
output application/json
---
{
"totPills": payload.report[0].totPills,
"report": payload.report map {
food: $.food,
drink: $.drink,
qty: ($ - "food" - "drink" - "totPills")
}
}