Home > Back-end >  JSON file - how to delete entire node that contains a string
JSON file - how to delete entire node that contains a string

Time:12-12

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.

CodePudding user response:

Based on your comment, you can remove that object having the property id = ToRemove by filtering where id is not equal to ToRemove and assigning that result to the .JSONS property:

$json = Get-Content path\to\json.json -Raw | ConvertFrom-Json
$json.JSONS = @($json.JSONS.Where{ $_.id -ne 'ToRemove' })
$json | ConvertTo-Json

The end result in this case would be an empty array for the .JSONS property:

{
  "JSONS": []
}

.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.

CodePudding user response:

You should be able to use just plain PowerShell, like this:

{
   "JSONS" : [ 
      { 
         "id" : "ToRemove", 
         "First" : [ 
            { 
               "id" : "geo", 
               "Name" : "Person1", 
               "model" : [ 
               ]
            }, 
            { 
               "id" : "Meters", 
               "Dictionary" : "4.2"
            }
         ]
      },
      { 
         "id" : "DontRemove", 
         "First" : []
      }
   ]
}
$json = Get-Content -Path $SourceFile  | ConvertFrom-Json
$json.JSONS = $json.JSONS | Where-Object { $_.Id -ne "ToRemove" }
$json | ConvertTo-Json -Depth 100 | Out-File -Path $DestFile
  • Related