PowerShell "ConvertFrom-Json: Invalid JSON primitive"
ConvertFrom-Json : Invalid JSON primitive: <something> reads like a JSON syntax error, and sometimes it is one — but just as often the JSON itself is fine and something else entirely is going on.
// cause 1 — non-JSON text mixed into the pipeline
--output json or similar).$result = some-cli-tool get-config $config = $result | ConvertFrom-Json
ConvertFrom-Json : Invalid JSON primitive: Loading.
some-cli-tool printed "Loading..." to the same output stream before its real JSON. $result is now the string "Loading...\n{ ... }" -- ConvertFrom-Json chokes on "Loading" before it ever reaches the { that starts the real JSON.$result = some-cli-tool get-config --output json # ask for JSON-only output if the CLI supports it # or, isolate the JSON manually if it doesn't: $json = ($result -split "`n" | Where-Object { $_ -match '^\s*[\{\[]' }) -join "`n" $config = $json | ConvertFrom-Json
// cause 2 — genuinely different parsers depending on PowerShell version
This isn't a minor detail — the two major PowerShell lines use entirely different JSON implementations, and they don't agree on what's valid:
ConvertFrom-Json is built on JavaScriptSerializer — stricter about some things. Historically rejects JSON containing comments outright, and this is the version that ships built into Windows, so it's the one most people hit without choosing toConvertFrom-Json is built on Newtonsoft Json.NET — more lenient. Accepts comments in the JSON, and generally tolerates input that 5.1 would reject outright$PSVersionTable.PSVersion in both is worth doing before anything else — it's frequently the actual explanation, not a subtle bug in the data.PS> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 1 19041 5486 <-- Windows PowerShell 5.1: JavaScriptSerializer, stricter # vs. 7 4 6 <-- PowerShell 7+: Newtonsoft Json.NET, more lenient
// frequently asked questions
What does "Invalid JSON primitive" actually mean in PowerShell?
It often has nothing to do with your JSON being malformed. The single most common cause is non-JSON text mixed into the pipeline — a progress spinner, a warning line, a login banner — landing in the same string before the real JSON. ConvertFrom-Json chokes on that leading text and reports it as the invalid primitive.
Why does the same JSON work in one PowerShell session but not another?
Windows PowerShell 5.1 and PowerShell 7+ use entirely different JSON implementations. 5.1's ConvertFrom-Json is built on JavaScriptSerializer and historically rejects JSON containing comments outright. PowerShell 7+ is built on Newtonsoft Json.NET and is more lenient. Check $PSVersionTable.PSVersion in both sessions before assuming it's a data problem.
How do I fix a CLI tool mixing status text with its JSON output?
Ask for JSON-only output if the CLI supports it (often a --output json flag). If it doesn't, isolate the JSON line manually before parsing, e.g. filtering the output to only lines starting with { or [ before piping to ConvertFrom-Json.
// try it yourself
Paste the raw string being handed to ConvertFrom-Json into the JSON Formatter — if it's genuinely malformed JSON rather than non-JSON text mixed in, this will point at the exact spot and can usually auto-fix it.
The single most common trigger: whatever produced the JSON also printed something else first — a progress spinner, a warning line, a login banner from a CLI tool — and it all lands in the same string before reaching
ConvertFrom-Json. The cmdlet sees that leading text, not the JSON that follows, and reports it as the invalid primitive. This shows up constantly piping output from CLI tools (Azure CLI, various REST wrappers) that mix status text with their JSON output on the same stream.