如何从载荷中移除空数组

huangapple go评论64阅读模式
英文:

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"
  }
}

huangapple
  • 本文由 发表于 2023年6月6日 01:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定