3

I have following JSON and I would like to remove streets from the JSON object under Address which is an array. I am trying to do this in powershell

{
  "Customer": [
    {
      "id": "123"
    }
  ],
  "Nationality": [
    {
      "name": "US",
      "id": "456"
    }
  ],
  "address": [
    {
      "$type": "Home",
      "name": "Houston",
      "streets": [
        {
          "name": "Union",
          "postalCode": "10",
        }
      ]
    },
    {
      "$type": "Office",
      "name": "Hawai",
      "streets": [
        {
          "name": "Rock",
          "postalCode": "11",
        }
      ]
    }
  ],
  "address": [
    {
      "$type": "Home1",
      "name": "Houston",
      "streets": [
        {
          "name": "Union1",
          "postalCode": "14",
        }
      ]
    },
    {
      "$type": "Office1",
      "name": "Hawaii1",
      "streets": [
        {
          "name": "Rock1",
          "postalCode": "15",
        }
      ]
    }
  ],
}

I would like to remove streets from the JSON object and here is my powershell script but it is not working! I am trying to convert JSON into object and then loop over properties to remove those.

$FileContent = Get-Content -Path "Test.json" -Raw | ConvertFrom-Json
foreach ($content in $FileContent) {
    #Write-Host $content.address
    $content.address = $content.address | Select-Object * -ExcludeProperty streets
}

$FileContent | ConvertTo-Json -Depth 100 | Out-File "Test.json" -Force

Learn AspNet
  • 1,192
  • 3
  • 34
  • 74

1 Answers1

6

When you use ConvertFrom-Json it will automatically convert things to objects for you. Once they're objects you can use Select-Object to specify what properties you want to include in the pipeline, with which you can just set $FileContent.address (an array of objects) to equal itself, excluding the streets property from each object in the array.

$FileContent = Get-Content -Path "Test.json" -Raw | ConvertFrom-Json
$FileContent.address = $FileContent.address | Select-Object * -ExcludeProperty streets
$FileContent | ConvertTo-Json
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • This is awesome but this does not work if I have multiple address blocks, it only keeps the last address without streets but removes the first address. I updated the JSON. Can you check how will it work in that case. Also if there are multiple customers with multiple addresses, will this work? – Learn AspNet Oct 01 '20 at 00:27
  • Your updated JSON is not valid. That's a duplicate key. Well, it has more issues than that, you have spare commas in it, which you had originally and I removed to test with, but the duplicate address key is not valid JSON. You can check your JSON at https://jsonlint.com/ – TheMadTechnician Oct 01 '20 at 00:46
  • I am having issues when streets property does not exist. Is there a way to check if the property exists before excluding the property – Learn AspNet Oct 05 '20 at 22:36
  • What kind of issues? You could loop through the array of objects and check each one I suppose. – TheMadTechnician Oct 06 '20 at 00:08
  • Can you answer this. https://stackoverflow.com/questions/64217264/only-remove-exclude-an-attribute-from-json-if-it-exists-in-powershell/64217468#64217468 – Learn AspNet Oct 06 '20 at 00:15