Mule-ESB-dataweave如何忽略null
在mule ESB dataweave中,我很难忽略空对象,Mule-ESB-dataweave如何忽略null,mule,esb,dataweave,Mule,Esb,Dataweave,在mule ESB dataweave中,我很难忽略空对象,{}。 我试图检查输入中是否存在特定的表。如果它存在,我会执行一些业务逻辑,如果它不存在,就不应该包含在输出中。然而,我得到的是{},而不是什么都没有 这是我的输入文件: { "srcTable": { "srcList": [ { "tableNames": "table1", "src": [ { "srcKey": [
{}
。
我试图检查输入中是否存在特定的表。如果它存在,我会执行一些业务逻辑,如果它不存在,就不应该包含在输出中。然而,我得到的是{}
,而不是什么都没有
这是我的输入文件:
{
"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"
},
{}
]
}
这是我的数据编织:
{
"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"
},
{}
]
}
此输出文件:
{
"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的帮助下,如果有人感兴趣,这就是解决方案。 将的大小与过滤器组合使用
%dw 1.0
%输出应用程序/json skipNullOn=“一切”
您好,谢谢您的回答,很遗憾,这两个解决方案都不起作用。第一个解决方案编译错误:在此情况下(sizeOf(payload.srcTable.srcList筛选器($.tableNames=='table1')!=0)为此,我添加了额外的括号以使其可编译,但是-我得到了无法将数字强制为布尔值的结果。第二个解决方案已编译,但仍然显示为空。两个解决方案都不起作用。请您建议我:)谢谢您和regardsHey Nivi,我继续删除了第一个建议(无论如何,我不会推荐它),并修复了我的第二个问题。此更新的答案应该对您有效。如果有效,请标记为已接受。感谢您稍加调整,它正在工作。感谢您的帮助非常感谢:)谢谢:)现在您的两个解决方案都有效:),谢谢您的帮助。保佑你,我已经挣扎了48小时了。上帝保佑你。谢谢你,问候Nivi