英文:
How to remove empty array from payload
问题
{
"Type": "User",
"id": "12345",
"meta": {
"lastUpdated": "2019-05-28T19:29:36"
},
"Relatioship": [{
"type": {
"coding": [{
"system": "xyz"
}]
},
"system": 99004
}],
"period": {
"start": "2010-10-12T00:00:00.000Z",
"end": "2010-10-12T00:00:00.000Z"
}
}
英文:
Below are the details-
My objective is to remove all keys having empty array, and all empty objects.
Sample Input
{
"Type": "User",
"id": "12345",
"meta": {
"userInfo": [],
"lastUpdated": "2019-05-28T19:29:36"
},
"Relatioship": [{
"type": {
"coding": [{
"system": "xyz"
}]
},
"system": 99004
}],
"period": {
"start": "2010-10-12T00:00:00.000Z",
"end": "2010-10-12T00:00:00.000Z"
},
"Contacts": [],
"Manager": [{
"type": {
"coding": []
}
}],
"team": [
[]
],
"account": [
[]
]
}
Is there any common function that can be used on any type of payloads to remove the empty arrays specifically like userInfo[] shouldn't be present in the end response.
Expected Output:
{
"Type": "User",
"id": "12345",
"meta": {
"lastUpdated": "2019-05-28T19:29:36"
},
"Relatioship": [{
"type": {
"coding": [{
"system": "xyz"
}]
},
"system": 99004
}],
"period": {
"start": "2010-10-12T00:00:00.000Z",
"end": "2010-10-12T00:00:00.000Z"
}
}
答案1
得分: 2
请注意,您不仅要删除空数组,还要删除由于删除数组而变为空的键。从这篇知识库文章中的方法非常灵活,可以用于此或其他用例。我封装了一个函数以便于重用。
%dw 2.0
output application/json
fun treeFilter(value: Any, predicate: (value:Any) -> Boolean) =
value match {
case object is Object -> do {
object mapObject ((value, key, index) ->
(key): treeFilter(value, predicate)
)
filterObject ((value, key, index) -> predicate(value))
}
case array is Array -> do {
array map ((item, index) -> treeFilter(item, predicate))
filter ((item, index) -> predicate(item))
}
else -> $
}
fun filterEmpty(value: Any) =
treeFilter (value, (value) ->
value match {
case v is Array| Object | Null | "" -> !isEmpty(v)
else -> true
})
---
filterEmpty(payload)
输出:
{
"Type": "User",
"id": "12345",
"meta": {
"lastUpdated": "2019-05-28T19:29:36"
},
"Relatioship": [
{
"type": {
"coding": [
{
"system": "xyz"
}
]
},
"system": 99004
}
],
"period": {
"start": "2010-10-12T00:00:00.000Z",
"end": "2010-10-12T00:00:00.000Z"
}
}
英文:
Note that you are not only want to remove empty arrays but also the keys that get empty because of removing the arrays. The method from this KB article is very flexible and can be used for this or other use cases. I encapsulated in a function for easier reuse.
%dw 2.0
output application/json
fun treeFilter(value: Any, predicate: (value:Any) -> Boolean) =
value match {
case object is Object -> do {
object mapObject ((value, key, index) ->
(key): treeFilter(value, predicate)
)
filterObject ((value, key, index) -> predicate(value))
}
case array is Array -> do {
array map ((item, index) -> treeFilter(item, predicate))
filter ((item, index) -> predicate(item))
}
else -> $
}
fun filterEmpty(value: Any) =
treeFilter (value, (value) ->
value match {
case v is Array| Object | Null | "" -> !isEmpty(v)
else -> true
})
---
filterEmpty(payload)
Output:
{
"Type": "User",
"id": "12345",
"meta": {
"lastUpdated": "2019-05-28T19:29:36"
},
"Relatioship": [
{
"type": {
"coding": [
{
"system": "xyz"
}
]
},
"system": 99004
}
],
"period": {
"start": "2010-10-12T00:00:00.000Z",
"end": "2010-10-12T00:00:00.000Z"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论