PowerShell Genie

PowerShell: invalid json input

Error 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Inspect the original response and JSON structure when ConvertFrom-Json cannot parse its input.

What it means

ConvertFrom-Json could not parse the supplied text. The response may be malformed JSON, an HTML error page or multiple documents combined into one string. Inspect the raw input and parser location before changing the conversion command.

Likely causes

  • The server replied with an HTML error page or a sign-in redirect
  • Get-Content was called without -Raw, so the file arrived as an array of separate lines
  • Several JSON documents were concatenated, which is valid NDJSON but not valid JSON
  • A trailing comma or an unquoted key survived a hand edit
  • A byte order mark or a stray console line was prepended to the payload

Check

1PS> $raw = Get-Content .\response.json -Raw
2PS> $raw | ConvertFrom-Json -ErrorAction Stop

Run these checks before changing anything.

What these checks establish

The file contents are not supplied by this example, so there is no universal result table. Inspect the response type and the parser location. HTML, one malformed document and newline-delimited JSON require different handling.

Fix

Read a complete JSON file with Get-Content -Raw. For newline-delimited JSON, parse each nonempty document line separately. Correct malformed source data at its producer where possible. For APIs, inspect status and content type before parsing. A larger ConvertTo-Json depth does not repair invalid JSON input.

Verify: Parse the corrected input and inspect the required fields, their types and any nested values; successful parsing alone does not validate the schema.

Reference

Microsoft Learn: convertfrom-json

Continue
Genie guidePowerShell: invalid json input

Inspect the original response and JSON structure when ConvertFrom-Json cannot parse its input.