Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

JSON · PowerShell Automation

Direct answer

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.

When to use this

Use this when PowerShell reads or writes JSON and nested data must not be truncated.

First 30 seconds

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.

Operational verification

JSON ↔ PowerShell objects

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
PowerShell 7.6/current
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformPowerShell 7.6/current · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when PowerShell reads or writes JSON and nested data must not be truncated.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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-Json default 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 references

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.

observation · 1
Key points
  • ConvertFrom-Json turns text into objects.
  • ConvertTo-Json -Depth avoids truncation.
  • Default depth of 2 silently cuts nested data.
Key checks
  • Get-Content f.json -Raw | ConvertFrom-Json — parse
  • $obj | ConvertTo-Json -Depth 10 — serialise
  • Test-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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
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
Technical examples

Configuration & expected output

Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.

Configuration example

PowerShell 7.6/current 7.6/currentMinimal authored PowerShell pattern for JSON

Assumptions: Review target identity, permissions and environment before mutation.

$json = $object | ConvertTo-Json -Depth 10
$copy = $json | ConvertFrom-Json
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docPowerShell 7.6/current 7.6/current

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%
Embedded reference data

Quick lookup tables

JSON · PowerShell operational reference

Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.

BoundaryJSON reference
Runtime identityPowerShell edition/version + command/module source
Persistent intentScript/module/config prerequisites under source control
VerificationRead-only discovery → execution evidence → independent target read-back
Output contractStructured objects/streams before formatting
Failure boundarySeparate engine/binding from transport/API/native and target-state failures
Depth when you need it

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.

Confirmation

Confirm action

Continue?