Bird
0
0

Given a PowerShell object with nested properties:

hard📝 Application Q9 of 15
PowerShell - File and Directory Operations
Given a PowerShell object with nested properties:
$obj = @{user = @{name = "Liam"; age = 34}; active = $true}
$json = $obj | ConvertTo-Json -Depth 2
$json

What is the correct output?
AError: Depth parameter invalid
B{"user":{"name":"Liam","age":34},"active":true}
C{"user":{"name":"Liam","age":34},"active":"true"}
D{"user":"Liam","age":34,"active":true}
Step-by-Step Solution
Solution:
  1. Step 1: Understand ConvertTo-Json with nested objects

    ConvertTo-Json serializes nested objects correctly when -Depth is set appropriately.
  2. Step 2: Check output format

    The nested user object is converted to JSON with its properties, and active boolean is converted to true without quotes.
  3. Final Answer:

    {"user":{"name":"Liam","age":34},"active":true} -> Option B
  4. Quick Check:

    Nested objects serialize correctly with -Depth [OK]
Quick Trick: Use -Depth to serialize nested objects fully [OK]
Common Mistakes:
  • Ignoring -Depth causes truncated output
  • Boolean values converted to strings incorrectly
  • Misplacing nested properties

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes