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

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

5 Comments

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?
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 jsonlint.com
I am having issues when streets property does not exist. Is there a way to check if the property exists before excluding the property
What kind of issues? You could loop through the array of objects and check each one I suppose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.