JSON operations let you easily change data between text and objects. This helps scripts talk with web services or save data in a simple format.
JSON operations (ConvertFrom-Json, ConvertTo-Json) in PowerShell
ConvertFrom-Json [-InputObject] <string> [<CommonParameters>] ConvertTo-Json [-InputObject] <psobject> [-Depth <int>] [<CommonParameters>]
ConvertFrom-Json turns JSON text into PowerShell objects you can work with.
ConvertTo-Json turns PowerShell objects into JSON text you can save or send.
$jsonText = '{"name":"Alice","age":30}'
$obj = $jsonText | ConvertFrom-Json
$obj.name$obj = @{name='Bob'; age=25}
$jsonText = $obj | ConvertTo-Json
$jsonText$jsonText = '[{"item":"apple","qty":5},{"item":"banana","qty":3}]' $list = $jsonText | ConvertFrom-Json $list[0].item
This script shows how to convert JSON text to an object, read and change data, then convert it back to JSON text.
$jsonString = '{"city":"Paris","temperature":18}' # Convert JSON text to object $weather = $jsonString | ConvertFrom-Json # Access properties Write-Output "City: $($weather.city)" Write-Output "Temperature: $($weather.temperature)°C" # Change temperature $weather.temperature = 20 # Convert back to JSON text $newJson = $weather | ConvertTo-Json Write-Output $newJson
Use the -Depth parameter with ConvertTo-Json if your object has nested objects deeper than 2 levels.
ConvertFrom-Json only works with valid JSON text. Invalid JSON will cause errors.
JSON keys become object properties you can access with dot notation.
ConvertFrom-Json changes JSON text into PowerShell objects.
ConvertTo-Json changes PowerShell objects into JSON text.
These commands help scripts work with data from web APIs or files easily.
Practice
ConvertFrom-Json do?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 AQuick Check:
ConvertFrom-Json = JSON text to objects [OK]
- Confusing ConvertFrom-Json with ConvertTo-Json
- Thinking it only formats JSON without conversion
- Assuming it validates JSON syntax only
$jsonText into a PowerShell object?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-Jsonis the correct way to pipe the JSON string into the cmdlet.Final Answer:
$jsonText | ConvertFrom-Json -> Option DQuick Check:
Pipe JSON string to ConvertFrom-Json [OK]
- Using ConvertTo-Json instead of ConvertFrom-Json
- Trying to call ConvertFrom-Json without pipeline incorrectly
- Confusing parameter order or cmdlet usage
$json = '{"name":"Alice","age":30}'
$obj = $json | ConvertFrom-Json
$obj.ageSolution
Step 1: Convert JSON string to object
The JSON string contains a property "age" with value 30 (number). ConvertFrom-Json creates an object with property age = 30.Step 2: Access the age property
Accessing$obj.agereturns the numeric value 30, not a string or error.Final Answer:
30 -> Option BQuick Check:
Property age value = 30 [OK]
- Expecting age as a string with quotes
- Thinking the output is the property name 'age'
- Assuming conversion error occurs
$json = '{"name":"Bob","age":25}'
$obj = ConvertFrom-Json $json
Write-Output $obj.nameSolution
Step 1: Convert JSON string to object
The JSON string contains a property "name" with value Bob (string). ConvertFrom-Json creates an object with property name = Bob.Step 2: Access the name property
Accessing$obj.namereturns the string value Bob, not a quoted string or error.Final Answer:
Bob -> Option AQuick Check:
Property name value = Bob [OK]
- Expecting output as a string with quotes
- Thinking the output is the property name 'name'
- Assuming positional argument causes conversion error
$person = @{ Name = "Eve"; Age = 28; Skills = @("PowerShell", "JSON") }Which command correctly converts this object to JSON text with indentation for readability?
Solution
Step 1: Understand ConvertTo-Json parameters
ConvertTo-Json converts objects to JSON text. The -Depth parameter controls how deep nested objects are converted.Step 2: Identify correct parameter for indentation
By default, ConvertTo-Json outputs indented JSON. The -Compress parameter removes whitespace, so it is not desired. There is no -Pretty parameter.Final Answer:
$person | ConvertTo-Json -Depth 2 -> Option CQuick Check:
Default output is indented JSON [OK]
- Using -Compress removes indentation
- Using non-existent -Pretty parameter
- Not setting -Depth for nested objects
Ready to prove it?
Pass the test and this pattern moves unknown
