- Courses
- đź’»Programming Languages
- 🪟PowerShell
- PowerShell for System Administration
- Working with APIs and Web Requests
- Parsing JSON and XML
On this page
PowerShell JSON and XML Parsing Guide
Updated on June 21, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
PowerShell provides native support for parsing both JSON and XML, the two dominant data formats used by REST APIs and enterprise web services. Invoke-RestMethod automatically parses JSON responses into PowerShell objects, eliminating manual conversion. When working with raw JSON from other sources, ConvertFrom-Json transforms JSON strings into custom objects or hashtables. For XML, the [xml] type accelerator converts XML text into a navigable document object, while Select-Xml enables powerful XPath queries for complex data extraction. Serializing PowerShell objects back to JSON is handled by ConvertTo-Json, which converts any .NET object to a JSON-formatted string. Mastering these tools is essential for building automation that reliably interacts with external services, configuration files, and modern APIs.
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.
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
Code explanation: $response.Content contains the raw JSON string. ConvertFrom-Json transforms it into PowerShell objects. $data now behaves like a collection of objects with properties. When reading JSON from a file, use Get-Content -Raw to read the file as a single string before piping to ConvertFrom-Json, as the -Raw parameter is required for ConvertFrom-Json to parse multi-line JSON correctly.
Accessing JSON Properties​
$data[0].name
$data[0].roles[1]
Code explanation: 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": "[email protected]",
"active": true
}
}
}
"@ | ConvertFrom-Json
$response.user.details.email
Code explanation: PowerShell preserves the nested structure, allowing you to traverse it using dot notation.
Handling Duplicate Keys​
The JSON standard allows duplicate key names, which are prohibited in PSObject and Hashtable types. When ConvertFrom-Json encounters duplicate keys, only the last key is used.
JSON Comments Support​
In Windows PowerShell 5.1, ConvertFrom-Json returns an error when it encounters JSON comments. In PowerShell 6 and later, the cmdlet supports JSON with comments, though comments are not included in the output object.
Parsing XML with PowerShell​
PowerShell treats XML as a structured document and exposes it through a .NET XML object model. Unlike JSON, PowerShell lacks a dedicated ConvertFrom-Xml cmdlet; using the [xml] type accelerator is the standard method.
Example: Parsing XML​
$response = Invoke-WebRequest -Uri "https://example.com/data.xml"
$xml = [xml]$response.Content
Code explanation: The [xml] cast operator converts the raw XML into an XML document. $xml now contains a hierarchical object tree based on the System.Xml.XmlDocument class.
Accessing XML Elements​
$xml.User.Name
$xml.User.Roles.Role
Code explanation: 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 through the Select-Xml cmdlet.
$xml.SelectNodes("//Role")
Code explanation: SelectNodes is a method of the XmlDocument object that executes an XPath query and returns matching nodes. The double slash (//) means "search at any level".
For more complex queries, Select-Xml provides additional flexibility:
Select-Xml -Path "C:\data\config.xml" -XPath "//Role"
Code explanation: The Select-Xml cmdlet lets you use XPath queries to search for text in XML strings and documents. You can specify the XML to search using the Content, Path, or Xml parameter.
Working with XML Namespaces​
When dealing with XML documents that use namespaces, you can specify a namespace hashtable with Select-Xml:
$ns = @{ns = "http://schemas.example.com/ns"}
Select-Xml -Path "data.xml" -XPath "//ns:Element" -Namespace $ns
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
Code explanation: The ConvertTo-Json cmdlet converts any .NET object to a string in JSON format. Properties are converted to field names, field values to property values, and methods are removed.
Important JSON Serialization Considerations​
Depth Limitation: By default, ConvertTo-Json only converts objects to a depth of 2 levels. For deeply nested objects, use the -Depth parameter to specify how many levels of contained objects are included. As of PowerShell 7.1, ConvertTo-Json emits a warning if the depth of the input object exceeds the depth specified, preventing unwanted data loss.
Compression: Use the -Compress parameter to remove extra whitespace from the JSON output, reducing the payload size.
Array Handling: Use the -AsArray parameter to wrap the output in array brackets, ensuring consistent array handling even for single objects.
Escape Handling: The -EscapeHandling parameter (PowerShell 6.2+) controls how special characters are handled in strings.
Converting to XML​
For creating XML representations of objects, PowerShell provides ConvertTo-Xml, which creates an XML-based representation of one or more .NET objects.
ConvertTo-Xml -InputObject (Get-Process) -Depth 3
Code explanation: The -Depth parameter specifies how many levels of contained objects are included in the XML representation. The default is 1.
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 -Depth 3
Code explanation: The -Depth 3 parameter ensures nested objects like DateTime are fully serialized. As of PowerShell 7.2, Extended Type System properties of DateTime and String objects are no longer serialized, and only the simple object is converted to JSON format.
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.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Invoke-RestMethod vs. Invoke WebRequest
Next
Active Directory
-
Understanding JSON and XML as Data Formats
- JSON (JavaScript Object Notation)
- XML (Extensible Markup Language)
-
Parsing JSON with PowerShell
- Example: Parsing JSON Manually
- Accessing JSON Properties
- Working with Nested JSON
- Handling Duplicate Keys
- JSON Comments Support
-
Parsing XML with PowerShell
- Example: Parsing XML
- Accessing XML Elements
- Working with XML Attributes
- Selecting XML Nodes with XPath
- Working with XML Namespaces
-
Converting PowerShell Objects Back to JSON or XML
- Converting to JSON
- Converting to XML
-
Practical Administrative Scenarios
- Parsing API Responses for Reporting
- Extracting Values from XML Configuration Files
- Combining JSON Parsing with Filtering
- Transforming PowerShell Objects into JSON for API Submission
-
Summary
