Use this when PowerShell reads or writes JSON and nested data must not be truncated.
JSON · PowerShell Automation
PowerShell handles JSON with ConvertFrom-Json to turn text into objects and ConvertTo-Json -Depth to serialize, since the default depth of 2 silently truncates nested data.
1. ConvertFrom/To-Json handle JSON.
2. Set -Depth (default 2 truncates).
3. -AsHashtable for dynamic keys (PS 7).
4. Validate with Test-Json.
5. UTF-8 encoding.
JSON ↔ PowerShell objects
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
Serialize a known fixture, validate the produced JSON with an independent parser/schema when available, parse it back and compare the required fields/types—not methods or incidental ETS metadata. Include nested structures at the maximum expected depth and test empty arrays/single-item arrays so enumeration behavior does not change the API contract.
Step 2 Common mistakes Narrow the fault
ConvertTo-Jsondefault depth of 2 truncating objects.
Step 3 Where it fails Narrow the fault
JSON validity does not show schema validity or semantic correctness. Round-tripping a PowerShell object does not preserve arbitrary methods/type behavior, and API consumers may distinguish null/missing/empty values that PowerShell code treats similarly. Keep PowerShell object shape, serialized text and external schema validation separate.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- ConvertFrom-Json turns text into objects.
- ConvertTo-Json -Depth avoids truncation.
- Default depth of 2 silently cuts nested data.
Get-Content f.json -Raw | ConvertFrom-Json— parse$obj | ConvertTo-Json -Depth 10— serialiseTest-Json -Json $json -Schema $schema— validate (PS 7)
ReferenceWhy it matters1 section · depth on demand
In depth
PowerShell converts between JSON and objects with ConvertFrom-Json and ConvertTo-Json. Objects from JSON behave like any other PowerShell object, so they can be filtered, modified and piped.
$cfg = Get-Content .\site.json -Raw | ConvertFrom-Json
$cfg.vlans | Where-Object id -ge 100
$cfg.vlans += [pscustomobject]@{ id = 130; name = 'CAMERAS' }
$cfg | ConvertTo-Json -Depth 10 | Set-Content .\site.json -Encoding utf8
Pitfalls: forgetting -Raw (Get-Content returns lines), the default depth of 2 in ConvertTo-Json, single-item arrays becoming scalars (use -AsArray or @()), and dates being converted automatically. ConvertFrom-Json -AsHashtable (PowerShell 7) is useful for keys with special characters or when order matters.
ReferenceImportant terms & boundaries1 section · depth on demand
PowerShell mechanics
ConvertTo-Json serializes object properties into JSON text and removes methods; ConvertFrom-Json parses JSON into PowerShell objects/hashtables according to version and parameters. Serialization depth, arrays versus scalar enumeration, property-name casing/duplicates, date handling and numeric precision can change the reconstructed representation, so JSON is a schema boundary rather than a transparent round trip for arbitrary PowerShell objects.
ReferenceConfiguration & state1 section · depth on demand
Authoring and lifecycle
Define the external JSON contract explicitly and select only properties that belong in it. Set -Depth high enough for the intended schema and treat warnings about truncation as correctness failures. When object ordering, duplicate keys or case-sensitive keys matter, choose the appropriate ConvertFrom-Json options and test against representative payloads across the supported PowerShell versions.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Configuration & expected output
Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.
Configuration example
Assumptions: Review target identity, permissions and environment before mutation.
$json = $object | ConvertTo-Json -Depth 10
$copy = $json | ConvertFrom-JsonVerification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Microsoft Learn documentation reviewed for bounded PowerShell reference claims. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.6 · confidence 94%Quick lookup tables
JSON · PowerShell operational reference
Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.
| Boundary | JSON reference |
|---|---|
| Runtime identity | PowerShell edition/version + command/module source |
| Persistent intent | Script/module/config prerequisites under source control |
| Verification | Read-only discovery → execution evidence → independent target read-back |
| Output contract | Structured objects/streams before formatting |
| Failure boundary | Separate engine/binding from transport/API/native and target-state failures |
Deep reference
Healthy vs suspicious evidence
Output interpretation
The console's object formatting after ConvertFrom-Json is not the wire payload. ConvertTo-Json produces a string, and depth limits can omit nested content while still producing valid JSON. Numbers and dates can be represented differently across versions or target systems, so inspect the actual JSON text at integration boundaries.