DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Fix “Unexpected Token” and Other Invalid JSON Errors

An “Unexpected token” error means the input could not be parsed as JSON. Find which operation failed, inspect its actual input, correct the syntax or response, and retry.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

First identify what is being parsed: JSON.parse(text) parses a string in your code, while response.json() parses an HTTP response body. An “Unexpected token” error means the input could not be parsed as JSON; inspect the actual input near the reported position, correct its syntax or find out what the server returned, then retry.

Find which operation failed

Read the stack trace or the line of code that raised the error. The parser’s input depends on the operation, so begin with the matching diagnostic path.

As an Amazon Associate I earn from qualifying purchases.

Operation What it parses First diagnostic move
JSON.parse(text) The exact string supplied by your program Inspect that string and the syntax around the reported location.
response.json() The HTTP response body Inspect the response body and status to establish what the server returned.
request.json() The HTTP request body Verify the request body is valid JSON; distinguish a parsing SyntaxError from body-state or decoding TypeError conditions.

MDN’s JSON.parse() reference documents malformed-string parsing errors. The Response.json() reference explains that the method reads and parses a response body, and the Request.json() reference documents request-body parsing and related exceptions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inspect the input at the failure point

For JSON.parse(text)

Inspect or log the exact string immediately before calling JSON.parse(). Check the parser’s reported position, line, or column, then read the surrounding characters and the preceding value or delimiter. The reported location narrows the search, but the mistake may be earlier—for example, a missing comma can make the next token appear unexpected.

For response.json()

In the browser’s Network panel, inspect the request’s status and response body. If needed, temporarily read the response as text while debugging so you can see what arrived instead of assuming the server returned JSON. An HTML error page, an empty body, or plain text are possible explanations, not conclusions: verify the actual body before deciding what to fix.

For request.json()

Check the body sent with the request and confirm that it is JSON text. A parsing SyntaxError means the body could not be parsed as JSON. A TypeError can instead indicate a body that is locked or disturbed, or a decoding problem; changing JSON punctuation will not resolve those cases.

Check for common JSON syntax errors

JSON is not the same as a JavaScript object literal. JSON property names and strings must use double quotes, and JavaScript conveniences such as trailing commas are not permitted. RFC 8259 specifies that a JSON parser must accept texts that conform to the JSON grammar; it does not make invalid input valid.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Trailing comma: [1, 2, 3, 4,] is invalid; use [1, 2, 3, 4]. Likewise, change {"foo": 1,} to {"foo": 1}.
  • Single quotes or unquoted property names: {'foo': 1} is invalid JSON. Use {"foo": 1}.
  • Comments: JSON does not allow comments. Remove them from the JSON text.
  • Missing punctuation: Check for missing commas between array items or object properties, and missing colons between a property name and its value.
  • Incomplete strings or invalid escapes: Close every string and ensure backslashes introduce valid JSON escape sequences.
  • Invalid numbers: A leading zero such as 01 is not allowed; use 1. A decimal point must be followed by digits, so 1. is invalid; use 1.0 if a fractional form is intended. Exponents must include their required digits.
  • Extra characters after the value: After the complete JSON value, only JSON whitespace may remain. Remove any additional text.

MDN documents examples such as SyntaxError: Unexpected token ] in JSON at position 13 and SyntaxError: Unexpected token } in JSON at position 12. Exact wording varies among JavaScript engines, so use the location as a clue and inspect the input rather than relying on a particular message format. See MDN’s JSON.parse bad-parsing guide and its JSON.parse examples.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Correct the source, then validate and retry

  1. Locate the source. For JSON.parse(), find the code, file, or data that produced the string. For a Fetch response, verify the request route, status, and body to determine whether the server returned the intended payload.
  2. Fix the producer or data. Correct the malformed text where it is created, stored, or returned. If a request is reaching the wrong route or the server is returning an error body, address that underlying issue rather than trying to parse the response as JSON.
  3. Check the corrected text against JSON grammar. A validator can help spot syntax errors, but it cannot tell you whether the data came from the right source or whether the response is the one your application expected.
  4. Repeat the same parse operation. Run the same code path with the corrected input. If it still fails, inspect the new input and error location; do not assume the first fix corrected every malformed character.

For the governing syntax and parser requirements, see RFC 8259, section 9.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.