Bird
Raised Fist0
PowerShellscripting~20 mins

JSON operations (ConvertFrom-Json, ConvertTo-Json) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JSON Mastery in PowerShell
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell command?
Given this JSON string and command, what will be the output?
PowerShell
$json = '{"name":"Alice","age":30}'; $obj = $json | ConvertFrom-Json; $obj.name
A{name=Alice; age=30}
BAlice
C30
DSystem.Object
Attempts:
2 left
💡 Hint
ConvertFrom-Json turns JSON text into an object you can access properties from.
💻 Command Output
intermediate
2:00remaining
What does this command output?
What is the output of this PowerShell command?
PowerShell
$obj = @{name='Bob'; age=25}; $json = $obj | ConvertTo-Json; $json
A
{
  "name": "Bob",
  "age": 25
}
Bname=Bob age=25
CSystem.String
D{"name":"Bob","age":25}
Attempts:
2 left
💡 Hint
ConvertTo-Json formats the object as a JSON string with indentation.
📝 Syntax
advanced
2:00remaining
Which option correctly parses nested JSON and accesses a nested property?
Given this JSON string, which command correctly outputs the city name?
PowerShell
$json = '{"person":{"name":"Eve","address":{"city":"Seattle","zip":"98101"}}}';
A$obj = ConvertFrom-Json $json; $obj.person.address.city
B$obj = $json | ConvertFrom-Json; $obj['person']['address']['city']
C$obj = $json | ConvertFrom-Json; $obj.person.address.city
D$obj = $json | ConvertTo-Json; $obj.person.address.city
Attempts:
2 left
💡 Hint
ConvertFrom-Json converts JSON text to an object. Use dot notation to access nested properties.
🔧 Debug
advanced
2:00remaining
Why does this command fail to output the expected JSON string?
This PowerShell code is intended to convert an object to JSON and print it, but it outputs nothing. What is the problem?
PowerShell
$obj = @{fruit='apple'; color='red'}; ConvertTo-Json $obj
AThe command outputs JSON but it is not captured or printed explicitly.
BConvertTo-Json cannot convert hashtables directly.
CThe variable $obj is empty, so no output is produced.
DConvertTo-Json requires the -Compress flag to output anything.
Attempts:
2 left
💡 Hint
In PowerShell, commands output to the pipeline but may not display if not captured or printed.
🚀 Application
expert
2:00remaining
How many items are in the resulting array after this JSON conversion?
Given this JSON string representing an array, how many elements does the PowerShell object contain after conversion?
PowerShell
$json = '[{"id":1,"val":"a"},{"id":2,"val":"b"},{"id":3,"val":"c"}]'; $arr = $json | ConvertFrom-Json; $arr.Count
AThrows an error
B1
C0
D3
Attempts:
2 left
💡 Hint
ConvertFrom-Json converts JSON arrays into PowerShell arrays with Count property.

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

  1. Step 1: Understand the purpose of ConvertFrom-Json

    This cmdlet takes JSON formatted text and turns it into PowerShell objects you can work with.
  2. 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.
  3. Final Answer:

    It converts JSON text into PowerShell objects. -> Option A
  4. 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

  1. 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.
  2. Step 2: Check the syntax for using pipeline

    Using $jsonText | ConvertFrom-Json is the correct way to pipe the JSON string into the cmdlet.
  3. Final Answer:

    $jsonText | ConvertFrom-Json -> Option D
  4. 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
  • Confusing parameter order or cmdlet usage
3. What is the output of this PowerShell code?
$json = '{"name":"Alice","age":30}'
$obj = $json | ConvertFrom-Json
$obj.age
medium
A. Error: Cannot convert string to object
B. 30
C. age
D. "30"

Solution

  1. 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.
  2. Step 2: Access the age property

    Accessing $obj.age returns the numeric value 30, not a string or error.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Property age value = 30 [OK]
Hint: ConvertFrom-Json creates objects; access properties directly [OK]
Common Mistakes:
  • Expecting age as a string with quotes
  • Thinking the output is the property name 'age'
  • Assuming conversion error occurs
4. What is the output of this PowerShell code?
$json = '{"name":"Bob","age":25}'
$obj = ConvertFrom-Json $json
Write-Output $obj.name
medium
A. Bob
B. "Bob"
C. name
D. Error: Cannot convert string to object

Solution

  1. 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.
  2. Step 2: Access the name property

    Accessing $obj.name returns the string value Bob, not a quoted string or error.
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    Property name value = Bob [OK]
Hint: ConvertFrom-Json creates objects; access properties directly [OK]
Common Mistakes:
  • Expecting output as a string with quotes
  • Thinking the output is the property name 'name'
  • Assuming positional argument causes conversion error
5. You have a PowerShell object:
$person = @{ Name = "Eve"; Age = 28; Skills = @("PowerShell", "JSON") }

Which command correctly converts this object to JSON text with indentation for readability?
hard
A. $person | ConvertTo-Json -Depth 2 -Compress
B. $person | ConvertTo-Json -Depth 2 -Pretty
C. $person | ConvertTo-Json -Depth 2
D. ConvertTo-Json $person -Depth 2 -Compress

Solution

  1. Step 1: Understand ConvertTo-Json parameters

    ConvertTo-Json converts objects to JSON text. The -Depth parameter controls how deep nested objects are converted.
  2. 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.
  3. Final Answer:

    $person | ConvertTo-Json -Depth 2 -> Option C
  4. Quick Check:

    Default output is indented JSON [OK]
Hint: Use ConvertTo-Json with -Depth; default output is indented [OK]
Common Mistakes:
  • Using -Compress removes indentation
  • Using non-existent -Pretty parameter
  • Not setting -Depth for nested objects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard