Parsing JSON and XML¶
When working with REST APIs or web services, the data you receive is almost always structured. Two formats dominate modern systems: JSON and XML. PowerShell provides first‑class support for both, allowing you to convert raw HTTP responses into rich, navigable objects. Understanding how to parse these formats is essential for building reliable automation that interacts with external services.
This section explains how JSON and XML are structured, how PowerShell interprets them, and how to work with the resulting objects in a clear and predictable way.
1. Understanding JSON and XML as Data Formats¶
Although JSON and XML serve similar purposes—representing structured data—they differ in syntax and philosophy.
JSON (JavaScript Object Notation)¶
- Lightweight and human‑readable
- Uses key–value pairs and arrays
- Dominant in modern REST APIs
- Maps naturally to PowerShell hashtables and objects
Example structure:
{
"name": "John",
"roles": ["admin", "editor"],
"active": true
}
XML (Extensible Markup Language)¶
- Hierarchical and tag‑based
- Common in older APIs and enterprise systems
- Supports attributes, nested elements, and schemas
- Maps to PowerShell’s XML document object model
Example structure:
<User>
<Name>John</Name>
<Roles>
<Role>admin</Role>
<Role>editor</Role>
</Roles>
<Active>true</Active>
</User>
PowerShell can parse both formats into structured objects that you can query and manipulate.
2. Parsing JSON with PowerShell¶
PowerShell includes two key cmdlets for working with JSON:
ConvertFrom-Json— converts JSON text into PowerShell objectsConvertTo-Json— converts PowerShell objects into JSON text
When you use Invoke-RestMethod, JSON is parsed automatically.
When you use Invoke-WebRequest, you must parse it manually.
Example: parsing JSON manually¶
$response = Invoke-WebRequest -Uri "https://api.example.com/v1/users"
$data = $response.Content | ConvertFrom-Json
Here:
$response.Contentcontains the raw JSON string.ConvertFrom-Jsontransforms it into PowerShell objects.$datanow behaves like a collection of objects with properties.
Accessing JSON properties¶
$data[0].name
$data[0].roles[1]
PowerShell maps JSON arrays to PowerShell arrays and JSON objects to custom objects, making navigation intuitive.
Working with nested JSON¶
$response = @"
{
"user": {
"name": "John",
"details": {
"email": "john@example.com",
"active": true
}
}
}
"@ | ConvertFrom-Json
$response.user.details.email
PowerShell preserves the nested structure, allowing you to traverse it using dot notation.
3. Parsing XML with PowerShell¶
PowerShell treats XML as a structured document and exposes it through a .NET XML object model. The primary cmdlet is:
[xml]cast operator — converts XML text into an XML document object
Example: parsing XML¶
$response = Invoke-WebRequest -Uri "https://example.com/data.xml"
$xml = [xml]$response.Content
Here:
- The
[xml]cast operator converts the raw XML into an XML document. $xmlnow contains a hierarchical object tree.
Accessing XML elements¶
$xml.User.Name
$xml.User.Roles.Role
XML elements become properties, and repeated elements become collections.
Working with XML attributes¶
XML supports attributes, which appear as properties on the element.
<User id="42">
<Name>John</Name>
</User>
Accessing the attribute:
$xml.User.id
Selecting XML nodes with XPath¶
PowerShell supports XPath queries for complex XML structures.
$xml.SelectNodes("//Role")
XPath is extremely powerful when working with deeply nested or irregular XML.
4. Converting PowerShell Objects Back to JSON or XML¶
When sending data to APIs, you often need to serialize PowerShell objects.
Converting to JSON¶
$body = @{
name = "John"
active = $true
} | ConvertTo-Json
This produces a JSON string suitable for API requests.
Converting to XML¶
$xml = New-Object System.Xml.XmlDocument
Or, for simple structures:
$object | ConvertTo-Json | ConvertFrom-Json | ConvertTo-Xml
XML serialization is less common in modern APIs but still relevant in enterprise environments.
5. Practical Administrative Scenarios¶
Parsing API responses for reporting¶
$data = Invoke-RestMethod -Uri "https://api.example.com/v1/servers"
$data | Select-Object name, status, uptime
Extracting values from XML configuration files¶
$config = [xml](Get-Content "C:\App\config.xml")
$config.Settings.Database.ConnectionString
Combining JSON parsing with filtering¶
$users = Invoke-RestMethod -Uri "https://api.example.com/v1/users"
$active = $users | Where-Object { $_.active -eq $true }
Transforming PowerShell objects into JSON for API submission¶
$payload = @{
title = "Backup completed"
timestamp = (Get-Date)
} | ConvertTo-Json
6. Summary¶
PowerShell provides robust, native support for parsing and generating JSON and XML, enabling seamless integration with modern REST APIs and traditional enterprise systems. By understanding how these formats map to PowerShell objects, you can:
- Retrieve structured data from APIs
- Navigate nested JSON and XML structures
- Serialize PowerShell objects for API requests
- Work with configuration files and service responses
- Build automation that interacts reliably with external systems
Mastering JSON and XML parsing is essential for any administrator or developer working with web services, cloud platforms, or modern automation workflows.