I'm using PowerShell to extract data from an API call, update it and then pass it back to the API.
What I would like to know is whether or not there is a simple way to modify the JSON object, to filter out all the properties which are not desired at any location within the JSON structure?
I've tried the following, however the resultant JSON only has the lowest level properties removed (ie. "p2")
$example = ConvertFrom-Json '{"a":{"p1": "value1"},"p2": "value2", "b":"valueb"}'
$exclude = "p1", "p2"
$clean = $example | Select-Object -Property * -ExcludeProperty $exclude
ConvertTo-Json $clean -Compress
Result => {"a":{"p1":"value1"},"b":"valueb"}
I would like to have all $exlude entries removed, regardless of where they are located within the JSON. Is there a simple solution?
Update
Here is another (more complicated) JSON example:
{
"a": {
"p1": "value 1",
"c": "value c",
"d": {
"e": "value e",
"p2": "value 3"
},
"f": [
{
"g": "value ga",
"p1": "value 4a"
},
{
"g": "value gb",
"p1": "value 4b"
}
]
},
"p2": "value 2",
"b": "value b"
}
The expected result (all p1 and p2 keys removed):
{
"a": {
"c": "value c",
"d": {
"e": "value e"
},
"f": [
{
"g": "value ga"
},
{
"g": "value gb"
}
]
},
"b": "value b"
}