I have this JSON and I want to remove the empty object inside value array using powershell. Anyone can help?
{ "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
The above yields:
{"value":[{"name":"a"}]}
See also:
.Remove() method on the $obj . Best way would probably be to re-cast the $obj in to a generic list $list=[System.Collections.Generic.List[System.Object]]::new() and then use the method $list.Remove($list[0])Where-Object Name -ne 'a'; if you need to combine both criteria: Where-Object { $_.psobject.Properties.Count -gt 0 -and $_.Name -ne 'a' }. Use -cne instead of -ne for case-sensitive matching. If you have further questions, please create a new question post.Where-Object filter is enough; please see my previous comment.