Mule ESB数据编织如何忽略空值
Mule ESB dataweave how to ignore null
在 mule ESB dataweave 中,我无法忽略空对象,{}
。
我正在尝试检查我的输入中是否存在特定的 table。如果它存在,我做一些业务逻辑,如果它不存在,它不应该包含在输出中。但是,我得到 {}
而不是什么都没有。
这是我的输入文件:
{
"srcTable": {
"srcList": [
{
"tableNames": "table1",
"src": [
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "withinYearTotalMaxSection",
"value": "2500"
},
{
"key": "previousClaimsTotalMaxSection",
"value": "25000"
},
{
"key": "previousClaimsTotalMax",
"value": "50000"
}
]
}
]
},
{
"tableNames": "table2",
"src": [
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "A"
},
{
"key": "garden",
"value": "1000"
},
{
"key": "risk",
"value": "50000"
}
]
},
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "B"
},
{
"key": "garden",
"value": "0"
},
{
"key": "risk",
"value": "50000"
}
]
}
]
},
{
"tableNames": "table3",
"src": [
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "GLD"
},
{
"key": "plants",
"value": "1500"
},
{
"key": "theft",
"value": "3000"
}
]
},
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "SVR"
},
{
"key": "plants",
"value": "0"
},
{
"key": "theft",
"value": "1000"
}
]
}
]
}
]
}
}
这是我的数据编织:
%dw 1.0
%output application/json skipNullOn="everything"
---
{
singlevalue: [
{
(payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date',
value: $.value
})
})
})
},
{
(payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date' when $.value != null otherwise null,
value: $.value
})
})
})
}
]
}
这个输出文件:
{
"singlevalue": [
{
"name": "date",
"value": "01/01/2016"
},
{}
]
}
谁能建议如何摆脱空对象,{}
?
谢谢和问候
NK
最简单的做法是删除末尾的所有空元素,如下所示:
%dw 1.0
%output application/json skipNullOn="everything"
%var transformation = [
{
(payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date',
value: $.value
})
})
})
},
{
(payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date' when $.value != null otherwise null,
value: $.value
})
})
})
}
]
%function removeEmptyObjects(e)
e filter $ != {}
---
{ singleValue: removeEmptyObjects(transformation) }
这输出:
{
"singleValue": [
{
"name": "date",
"value": "01/01/2016"
}
]
}
在 Josh 的帮助下,如果有人感兴趣,这就是解决方案。
使用尺寸 Of 和过滤器组合
%dw 1.0
%output application/json skipNullOn="everything"
{
singlevalue: [
({
(payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date',
value: $.value
})
})
})
}) when (sizeOf (payload.srcTable.srcList filter $.tableNames == 'table1')) != 0,
({
(payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date' when $.value != null otherwise null,
value: $.value
})
})
})
}) when (sizeOf (payload.srcTable.srcList filter $.tableNames == 'xxx')) != 0]}
在 mule ESB dataweave 中,我无法忽略空对象,{}
。
我正在尝试检查我的输入中是否存在特定的 table。如果它存在,我做一些业务逻辑,如果它不存在,它不应该包含在输出中。但是,我得到 {}
而不是什么都没有。
这是我的输入文件:
{
"srcTable": {
"srcList": [
{
"tableNames": "table1",
"src": [
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "withinYearTotalMaxSection",
"value": "2500"
},
{
"key": "previousClaimsTotalMaxSection",
"value": "25000"
},
{
"key": "previousClaimsTotalMax",
"value": "50000"
}
]
}
]
},
{
"tableNames": "table2",
"src": [
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "A"
},
{
"key": "garden",
"value": "1000"
},
{
"key": "risk",
"value": "50000"
}
]
},
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "B"
},
{
"key": "garden",
"value": "0"
},
{
"key": "risk",
"value": "50000"
}
]
}
]
},
{
"tableNames": "table3",
"src": [
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "GLD"
},
{
"key": "plants",
"value": "1500"
},
{
"key": "theft",
"value": "3000"
}
]
},
{
"srcKey": [
{
"key": "date",
"value": "01/01/2016"
},
{
"key": "type",
"value": "SVR"
},
{
"key": "plants",
"value": "0"
},
{
"key": "theft",
"value": "1000"
}
]
}
]
}
]
}
}
这是我的数据编织:
%dw 1.0
%output application/json skipNullOn="everything"
---
{
singlevalue: [
{
(payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date',
value: $.value
})
})
})
},
{
(payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date' when $.value != null otherwise null,
value: $.value
})
})
})
}
]
}
这个输出文件:
{
"singlevalue": [
{
"name": "date",
"value": "01/01/2016"
},
{}
]
}
谁能建议如何摆脱空对象,{}
?
谢谢和问候 NK
最简单的做法是删除末尾的所有空元素,如下所示:
%dw 1.0
%output application/json skipNullOn="everything"
%var transformation = [
{
(payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date',
value: $.value
})
})
})
},
{
(payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date' when $.value != null otherwise null,
value: $.value
})
})
})
}
]
%function removeEmptyObjects(e)
e filter $ != {}
---
{ singleValue: removeEmptyObjects(transformation) }
这输出:
{
"singleValue": [
{
"name": "date",
"value": "01/01/2016"
}
]
}
在 Josh 的帮助下,如果有人感兴趣,这就是解决方案。 使用尺寸 Of 和过滤器组合
%dw 1.0
%output application/json skipNullOn="everything"
{
singlevalue: [
({
(payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date',
value: $.value
})
})
})
}) when (sizeOf (payload.srcTable.srcList filter $.tableNames == 'table1')) != 0,
({
(payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
(r.src map {
($.srcKey filter ($.key == 'date') map {
name: 'date' when $.value != null otherwise null,
value: $.value
})
})
})
}) when (sizeOf (payload.srcTable.srcList filter $.tableNames == 'xxx')) != 0]}