I have a JSON
file:
{
"JSONS" : [
{
"id" : "ToRemove",
"First" : [
{
"id" : "geo",
"Name" : "Person1",
"model" : [
],
"adjustments" : [
{
"uid" : "3",
"name" : "4s",
"value" : "1"
},
{
"uid" : "5",
"name" : "3s",
"value" : "6"
}
]
},
{
"id" : "Meters",
"Dictionary" : "4.2"
},
{
"id" : "Moon",
"Filter" : "0.5",
"Saturn" : {
"s" : "0",
"v" : "1"
}
}
]
}
]
}
I would like to delete entire node, if the "id"
, in this example, contains "ToRemove"
string. Everyting between {
and },
including those lines also, to make the final JSON
consistent.
This is a screenshot what I want to get rid of.
I only found how to delete properties, but not entire nodes. I’ve tried to appli something like this:
$ToRemove = Get-Content $SourceFile | ConvertFrom-Json
$ToRemove.PSObject.Object.Remove('id:','ToRemove')
$ToRemove | ConvertTo-Json -Depth 100 | Out-File $DestFile
but of course it didn’t work.
How to delete the entire node? I would love to use an array to put all strings I would like to delete.
2
Answers
Based on your comment, you can remove that object having the property
id = ToRemove
by filtering whereid
is not equal toToRemove
and assigning that result to the.JSONS
property:The end result in this case would be an empty array for the
.JSONS
property:.PSObject.Properties.Remove(...)
wouldn’t be useful in this case because what it does is remove properties from one object but what you want to do is filter out an entire object based on a condition.You should be able to use just plain PowerShell, like this: