convertfrom-json — Parse JSON into pipeable PowerShell objects across all 5 shells
Equivalents in every shell
curl -s https://api.example.com/users | jq '.[] | .name'`jq` is the canonical bash JSON parser. `.[]` iterates arrays; `.field` accesses properties; `select(.role == "admin")` filters. `jq -r` strips the surrounding quotes from string output for piping into other shell tools. Install via apt / brew / dnf — usually a separate package, not coreutils.
curl -s https://api.example.com/users | jq '.[] | .name'Same external `jq`. macOS needs `brew install jq` — no system jq. Zsh's `${(f)var}` line-split modifier is useful when parsing jq's newline-separated raw output back into a zsh array.
curl -s https://api.example.com/users | jq '.[] | .name'Same external `jq`. Fish's `string split \n` builtin pairs naturally with `jq -r` output to produce a fish list that can be iterated cleanly via `for x in $list`.
Get-Content users.json | ConvertFrom-Json | ForEach-Object { $_.name }Takes a JSON string on the pipeline (or via `-InputObject`) and returns a `[PSCustomObject]` graph that you can drill into with `.field` syntax. Arrays become PowerShell arrays — `($json | ConvertFrom-Json)[0]` indexes. The cmdlet auto-detects single-object vs array input.
powershell -NoProfile -Command "Get-Content users.json | ConvertFrom-Json | ForEach-Object { $_.name }"cmd.exe has no JSON parser. Shell out to PowerShell (Win 7+ ships 5.1) — the `-NoProfile` flag skips $PROFILE for faster cold start. For one-shot parsing without PowerShell, install `jq` via `winget install jqlang.jq` and use the bash-style syntax.
Worked examples
Parse a JSON API response and select one field
curl -s https://api.example.com/users/1 | jq -r .name(Invoke-RestMethod https://api.example.com/users/1).nameFilter an array by property and count matches
cat users.json | jq '[.[] | select(.role == "admin")] | length'(Get-Content users.json | ConvertFrom-Json | Where-Object role -eq admin).CountPreserve case-sensitive keys (PowerShell 6+ only)
echo '{"Name":"a","name":"b"}' | jq 'keys''{"Name":"a","name":"b"}' | ConvertFrom-Json -AsHashtable | ForEach-Object { $_.Keys }Gotchas
- Default output is `[PSCustomObject]` whose property keys are **case-INSENSITIVE** — `$obj.Name` and `$obj.name` both work. If your JSON has distinct `"Name"` and `"name"` keys, ONE of them silently shadows the other. Use `-AsHashtable` (pwsh 6+) to preserve case-sensitive keys (returns `[OrderedDictionary]`).
- Single-element JSON arrays UNWRAP by default — `'[1]' | ConvertFrom-Json` returns `1` (a number), not `@(1)` (an array). This breaks `.Count` / `.Length` / `foreach` consumers that expect an array. Use `-NoEnumerate` (pwsh 6+) to keep single-element arrays as arrays, or wrap defensively: `@($obj | ConvertFrom-Json)`.
- Duplicate keys: pwsh 5.1 throws on duplicate keys (`Cannot process argument because the value of argument "name" is not valid`). pwsh 7 silently overwrites with the LAST value. pwsh 7.3+ added `-DateKind` and stricter defaults — always pin pwsh version in CI for JSON-heavy scripts.
- Dates: ISO 8601 strings that LOOK like dates are AUTO-CONVERTED to `[DateTime]` on pwsh 5.1 and early 7.x — surprising when the field was meant to stay as a string identifier. pwsh 7.4+ added `-DateKind String` to opt out; on older versions, the workaround is `-AsHashtable` + manual parsing.
- Large JSON files: `Get-Content file.json` reads the file line-by-line and concatenates — slow on multi-MB JSON. Use `Get-Content -Raw file.json | ConvertFrom-Json` (single buffered read) for 10-100× speed-up. For multi-GB JSON, neither approach scales — fall back to `jq --stream` or a JSON streaming library.
WSL & PowerShell Core notes
Common tasks using convertfrom-json
- Minify a JSON file
Strip whitespace and indentation from a JSON file to produce the smallest valid output.
- Parse JSON from a shell
Extract specific fields from a JSON blob (a curl response, kubectl output, log line) and pipe them into other shell tools — the everyday "grab the .token / .items[].name / .data.url" workflow that differs sharply from just pretty-printing.