PowerShell iterate through json of key value pairs

Viewed 1560

I have a json file that maps alpha-2 codes to country names:

{
    "AF": "Afghanistan",
    "EG": "Ägypten",
    ...
}

I want to iterate over each key value pair to process the data. How would I be able to achieve that?

I tried the following but it always returns one big object that is not iterable in a key value manner:

$json = Get-Content -Path C:\countries.json | ConvertFrom-Json
1 Answers

I was able to achieve it like this:

foreach ($obj in $json.PSObject.Properties) {
    $obj.Name
    $obj.Value
}
Related