Selected topic
Data Handling
Prefer practical output? Use related tools below while reading.
JSON (JavaScript Object Notation) is a lightweight data interchange format that is widely used to exchange data between web servers, web applications, and mobile apps. In PowerShell, you can work with JSON using the ConvertTo-Json cmdlet to convert objects to JSON strings, and the ConvertFrom-Json cmdlet to parse JSON strings back into objects.
powershell
# Create an object
$person = [PSCustomObject]@{
Name = "John"
Age = 30
City = "New York"
}# Convert the object to JSON
$jsonString = $person | ConvertTo-Json
Write-Host $jsonString
Output:
json
{"Name":"John","Age":30,"City":"New York"}powershell
# Define a JSON string
$jsonString = '{"Name":"Jane","Age":25,"City":"Chicago"}'# Parse the JSON string back into an object
$person = $jsonString | ConvertFrom-Json
Write-Host $person.Name
Output:
JaneConvertTo-Json to convert PowerShell objects to JSON strings when exchanging data with web servers or other systems.ConvertFrom-Json to parse JSON strings back into PowerShell objects when receiving data from external sources.[System.Text.Json] namespace for working with JSON in .NET Core and later versions.