📖 Scenario: You work in a small company that stores employee information in JSON format. You want to read this data, update it, and then save it back as JSON.
🎯 Goal: Learn how to convert JSON text into PowerShell objects using ConvertFrom-Json, modify the data, and then convert it back to JSON text using ConvertTo-Json.
📋 What You'll Learn
Create a JSON string variable with employee data
Convert the JSON string to a PowerShell object
Add a new employee to the object
Convert the updated object back to JSON and display it
💡 Why This Matters
🌍 Real World
Many systems use JSON to store and exchange data. PowerShell scripts often need to read, update, and write JSON data for automation tasks.
💼 Career
Understanding JSON operations in PowerShell is useful for IT administrators, DevOps engineers, and automation specialists who manage configurations and data in JSON format.
Progress0 / 4 steps
1
Create a JSON string with employee data
Create a variable called jsonData and assign it this exact JSON string: '{"employees":[{"name":"Alice","age":30},{"name":"Bob","age":25}]}'
PowerShell
Hint
Use single quotes around the JSON string to avoid escaping double quotes inside.
2
Convert JSON string to PowerShell object
Use ConvertFrom-Json to convert jsonData into a variable called employeeObj.
PowerShell
Hint
Use $employeeObj = ConvertFrom-Json $jsonData to convert the JSON string.
3
Add a new employee to the object
Add a new employee with name "Charlie" and age 28 to the employees array inside employeeObj.
PowerShell
Hint
Use $employeeObj.employees += @{name='Charlie'; age=28} to add the new employee.
4
Convert the updated object back to JSON and display it
Use ConvertTo-Json to convert employeeObj back to JSON and print it with Write-Output.
PowerShell
Hint
Use $jsonOutput = ConvertTo-Json $employeeObj and then Write-Output $jsonOutput.
Practice
(1/5)
1. What does the PowerShell cmdlet ConvertFrom-Json do?
easy
A. It converts JSON text into PowerShell objects.
B. It converts PowerShell objects into JSON text.
C. It formats JSON text for better readability.
D. It validates JSON syntax without conversion.
Solution
Step 1: Understand the purpose of ConvertFrom-Json
This cmdlet takes JSON formatted text and turns it into PowerShell objects you can work with.
Step 2: Differentiate from ConvertTo-Json
ConvertTo-Json does the opposite by turning objects into JSON text, so it is not the correct answer here.
Final Answer:
It converts JSON text into PowerShell objects. -> Option A
Quick Check:
ConvertFrom-Json = JSON text to objects [OK]
Hint: ConvertFrom-Json = JSON text to objects [OK]
Common Mistakes:
Confusing ConvertFrom-Json with ConvertTo-Json
Thinking it only formats JSON without conversion
Assuming it validates JSON syntax only
2. Which of the following is the correct syntax to convert a JSON string stored in variable $jsonText into a PowerShell object?
easy
A. ConvertTo-Json $jsonText
B. $jsonText | ConvertTo-Json
C. ConvertFrom-Json $jsonText -Compress
D. $jsonText | ConvertFrom-Json
Solution
Step 1: Identify the correct cmdlet for JSON to object conversion
ConvertFrom-Json converts JSON text to PowerShell objects, so it is the right cmdlet.
Step 2: Check the syntax for using pipeline
Using $jsonText | ConvertFrom-Json is the correct way to pipe the JSON string into the cmdlet.
Final Answer:
$jsonText | ConvertFrom-Json -> Option D
Quick Check:
Pipe JSON string to ConvertFrom-Json [OK]
Hint: Pipe JSON string to ConvertFrom-Json for conversion [OK]
Common Mistakes:
Using ConvertTo-Json instead of ConvertFrom-Json
Trying to call ConvertFrom-Json without pipeline incorrectly