How to Parse JSON in PowerShell
PowerShell has built-in ConvertFrom-Json and ConvertTo-Json cmdlets. Convert JSON text to objects and back, accessing properties with dot notation.
Parse a JSON string
Pipe or pass the JSON to ConvertFrom-Json; the result is an object with typed properties.
powershell
$text = '{"name":"Ada","roles":["admin","dev"]}'
$data = $text | ConvertFrom-Json
$data.name # Ada
$data.roles[0] # adminRead JSON from a file or API
Get-Content or Invoke-RestMethod feed straight into ConvertFrom-Json (Invoke-RestMethod even parses automatically).
powershell
$data = Get-Content data.json -Raw | ConvertFrom-Json
# Invoke-RestMethod parses JSON responses for you
$user = Invoke-RestMethod https://api.example.com/userConvert objects back to JSON
ConvertTo-Json serializes; raise -Depth for nested objects (the default is 2).
powershell
$obj = @{ name = "Ada"; active = $true }
$obj | ConvertTo-Json
$obj | ConvertTo-Json -Depth 10 # for deeply nested data