skip to Main Content

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


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

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search