ConvertFrom-Json

Understanding the Power of ConvertFrom-Json in PowerShell

Welcome back to Wahmans PowerShell Blog! Today, we’re diving into the ConvertFrom-Json cmdlet — a powerful tool in PowerShell that simplifies the transformation of JSON-formatted strings into PowerShell objects. As per Microsoft’s documentation:

ConvertFrom-Json – Converts a JSON-formatted string to a custom object or a hash table.

ConvertFrom-Json is especially useful when you’re working with REST APIs, configuration files, or any data source that presents information in JSON. Let’s explore a few practical use cases of this cmdlet — starting simple and ramping up to more advanced scenarios.

Example 1: Converting a Basic JSON String

$json = '{"Name": "Alice", "Age": 29}'
$object = $json | ConvertFrom-Json

# Access properties
Write-Output $object.Name  # Output: Alice
Write-Output $object.Age   # Output: 29

This is the most basic usage. You receive a JSON string, pipe it to ConvertFrom-Json, and it returns a PowerShell object with accessible properties.

Example 2: Process a JSON Array

$jsonArray = '[{"Name":"Alice"}, {"Name":"Bob"}, {"Name":"Charlie"}]'
$people = $jsonArray | ConvertFrom-Json

foreach ($person in $people) {
    Write-Output $person.Name
}

This time we have a JSON array that ConvertFrom-Json transforms into an array of PowerShell objects. Perfect for data iteration!

Example 3: Parsing Nested JSON Objects

$nestedJson = '{
  "Name": "Alice",
  "Contact": {
    "Email": "[email protected]",
    "Phone": "123-456-7890"
  }
}'

$person = $nestedJson | ConvertFrom-Json
Write-Output $person.Contact.Email  # Output: [email protected]

With nested JSON structures, ConvertFrom-Json keeps everything nice and accessible via dot notation. You can traverse deeply nested objects just like regular PowerShell custom objects.

Example 4: Consuming an API and Converting JSON Response

$response = Invoke-RestMethod -Uri "https://jsonplaceholder.typicode.com/users"

# If API only returns the JSON string instead of objects, use ConvertFrom-Json explicitly
$usersJsonString = Invoke-WebRequest -Uri "https://jsonplaceholder.typicode.com/users" | Select-Object -ExpandProperty Content
$users = $usersJsonString | ConvertFrom-Json

# Work with the parsed objects
foreach ($user in $users) {
    Write-Output "$($user.name) - $($user.email)"
}

When working with REST APIs, you’ll often receive JSON data. Even though Invoke-RestMethod typically converts JSON into objects automatically, in cases where you use Invoke-WebRequest or receive raw content, ConvertFrom-Json comes in handy.

Summary

ConvertFrom-Json is an essential cmdlet in any PowerShell scripter’s toolbox. It allows you to seamlessly convert JSON data into native PowerShell objects, making it a reliable workhorse in managing dynamic data structures from APIs, configurations, or other services.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *