0
0
PowerShellscripting~10 mins

JSON operations (ConvertFrom-Json, ConvertTo-Json) in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert a JSON string to a PowerShell object.

PowerShell
$jsonString = '{"name":"Alice","age":30}'
$obj = $jsonString | [1]
Drag options to blanks, or click blank then click option'
AConvertFrom-Json
BConvertTo-Json
COut-String
DConvertTo-Xml
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConvertTo-Json instead of ConvertFrom-Json.
Trying to use Out-String which just formats output as text.
2fill in blank
medium

Complete the code to convert a PowerShell object to a JSON string.

PowerShell
$obj = @{name='Bob'; age=25}
$jsonString = $obj | [1]
Drag options to blanks, or click blank then click option'
AOut-File
BConvertFrom-Json
CConvertTo-Json
DConvertTo-Csv
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConvertFrom-Json which does the opposite conversion.
Using Out-File which writes to a file, not convert to JSON.
3fill in blank
hard

Fix the error in the code to correctly convert JSON string to object.

PowerShell
$jsonString = '{"city":"Paris","population":2140000}'
$obj = [1] $jsonString
Drag options to blanks, or click blank then click option'
AGet-Content
BConvertFrom-Json
COut-String
DConvertTo-Json
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConvertTo-Json instead of ConvertFrom-Json.
Trying to use Get-Content which reads files, not convert JSON strings.
4fill in blank
hard

Fill both blanks to create a JSON string from an object and then convert it back to an object.

PowerShell
$obj = @{fruit='Apple'; color='Red'}
$json = $obj | [1]
$newObj = $json | [2]
Drag options to blanks, or click blank then click option'
AConvertTo-Json
BConvertFrom-Json
COut-String
DGet-Content
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the cmdlets order.
Using Out-String which does not convert JSON.
5fill in blank
hard

Fill all three blanks to convert a JSON string to object, modify it, and convert back to JSON.

PowerShell
$jsonString = '{"animal":"Dog","age":5}'
$obj = $jsonString | [1]
$obj.age = $obj.age + 1
$newJson = $obj | [2]
Write-Output [3]
Drag options to blanks, or click blank then click option'
AConvertFrom-Json
BConvertTo-Json
C$newJson
D$obj
Attempts:
3 left
💡 Hint
Common Mistakes
Using ConvertTo-Json first instead of ConvertFrom-Json.
Outputting the object instead of the JSON string.