簡體   English   中英

使用 powershell 刪除 JSON 數組中的空 Object

[英]Remove Empty Object in JSON Array using powershell

我有這個 JSON,我想使用 powershell 刪除值數組中的空 object。有人可以幫忙嗎?

{ "value" : [{},{},{"name": "a"}] }
# Parse the JSON into an object graph.
$obj = '{ "value" : [{},{},{"name": "a"}] }' | ConvertFrom-Json

# Filter out the empty objects, by counting the number of properties
# via the .psobject.Properties collection, available on any object in PowerShell.
# Note the need for @(...) to ensure that the result of the filtering remains
# an array.
$obj.Value = @($obj.Value | Where-Object { $_.psobject.Properties.Count -gt 0 })

# Reconvert to JSON.
$obj | ConvertTo-Json -Compress

以上產量:

{"value":[{"name":"a"}]}

也可以看看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM