0
0
PowerShellscripting~10 mins

JSON operations (ConvertFrom-Json, ConvertTo-Json) in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JSON operations (ConvertFrom-Json, ConvertTo-Json)
Start with JSON string
Use ConvertFrom-Json
Get PowerShell object
Modify or use object
Use ConvertTo-Json
Get JSON string output
End
This flow shows how a JSON string is turned into a PowerShell object, used or changed, then turned back into JSON text.
Execution Sample
PowerShell
$json = '{"name":"Anna","age":30}'
$obj = $json | ConvertFrom-Json
$obj.age = 31
$newJson = $obj | ConvertTo-Json
$newJson
This code converts a JSON string to an object, changes the age, then converts it back to JSON.
Execution Table
StepActionInputOutputNotes
1Assign JSON string to $json{"name":"Anna","age":30}{"name":"Anna","age":30}Raw JSON text stored
2ConvertFrom-Json to object$jsonObject with properties name='Anna', age=30JSON parsed into PowerShell object
3Modify object property$obj.age = 31Object with age=31Age property updated
4ConvertTo-Json from object$obj{ "name": "Anna", "age": 31 }Object converted back to JSON string
5Output $newJson$newJson{ "name": "Anna", "age": 31 }Final JSON string output
💡 Process ends after outputting the updated JSON string.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
$json{"name":"Anna","age":30}{"name":"Anna","age":30}{"name":"Anna","age":30}{"name":"Anna","age":30}
$objnullObject{name='Anna', age=30}Object{name='Anna', age=31}Object{name='Anna', age=31}
$newJsonnullnullnull{ "name": "Anna", "age": 31 }
Key Moments - 2 Insights
Why do we need ConvertFrom-Json before changing properties?
Because JSON text is just a string. ConvertFrom-Json turns it into an object we can change, as shown in step 2 and 3.
What happens if we skip ConvertTo-Json at the end?
Without ConvertTo-Json, we keep the object, not JSON text. Step 4 shows converting back to JSON string for output or saving.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $obj.age after step 3?
A30
B31
Cnull
DUndefined
💡 Hint
Check the 'Output' column in row for step 3 in execution_table.
At which step does the JSON string become a PowerShell object?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look for 'ConvertFrom-Json' action in execution_table.
If we change $obj.name to 'Bob' after step 3, what will $newJson contain after step 4?
A{"name":"Bob","age":31}
B{"name":"Bob","age":30}
C{"name":"Anna","age":31}
D{"name":"Anna","age":30}
💡 Hint
Think about the object properties before ConvertTo-Json in step 4.
Concept Snapshot
ConvertFrom-Json: turns JSON text into a PowerShell object.
Modify object properties directly.
ConvertTo-Json: turns object back into JSON text.
Use these to read, change, and save JSON data in scripts.
Full Transcript
This lesson shows how to work with JSON in PowerShell. First, you start with a JSON string. Using ConvertFrom-Json, you turn that string into a PowerShell object. This object lets you access and change data easily, like changing the age property. After changes, use ConvertTo-Json to turn the object back into a JSON string. This string can be saved or sent somewhere. The execution table traces each step: storing the JSON string, converting it to an object, changing the object, converting back to JSON, and outputting the result. Variables like $json, $obj, and $newJson change as the script runs. Key moments explain why conversion is needed before and after changes. The quiz checks understanding of when and how these conversions happen and what the final JSON looks like after changes.