My Profile Photo

[Doug Finke@dfinke]


Researching the optimal; Implementing the practical

Author of 'PowerShell for Developers'


PowerShell is a Query Language for JSON

PowerShell, as is, has all the tools you need to query JSON and then reshape, re-purpose it to what you need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$json = @"
{
  "locations": [
    {"name": "Seattle", "state": "WA"},
    {"name": "New York", "state": "NY"},
    {"name": "Bellevue", "state": "WA"},
    {"name": "Olympia", "state": "WA"}
  ]
}
"@ | ConvertFrom-Json

$names=(($json.locations | ? state -eq 'wa').name | Sort) -join ','    

@{WashingtonCities = $names} | ConvertTo-Json

Result

1
2
3
{
    "WashingtonCities":  "Bellevue,Olympia,Seattle"
}