How to Parse JSON in PowerShell

← All guides

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]    # admin

Read 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/user

Convert 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

Frequently Asked Questions

ConvertTo-Json defaults to a depth of 2. Pass -Depth with a higher number to serialize deeply nested objects fully.

No. ConvertFrom-Json and ConvertTo-Json are built into PowerShell 3.0+ and PowerShell Core.

Use Invoke-RestMethod, which fetches the URL and automatically converts the JSON response into PowerShell objects.

Related Tools