Free · Fast · Privacy-first

Fix JSON Unexpected Token

Unexpected token is the most common JSON parse error message you will encounter, and it is also the most under-explained.

Shows exactly which character is unexpected

🔒

Explains why each token type causes an error

Reports line and column, not just position offset

Free, browser-based, no installation

Cost
Free forever
Sign-up
Not required
Processing
In your browser
Privacy
Files stay local
FreeNo signupWhite-label

Add this JSON Validator to your website

Drop the JSON Validator into any page — blog post, product docs, intranet, school portal — with a single line of HTML. Your visitors get the full tool, processed entirely in their browser. No backend, no uploads, no signup.

  • Files stay 100% in the visitor's browser
  • Responsive — adapts to any container width
  • Free forever, no API key needed

Embed code

<iframe
  src="https://www.fixtools.io/json/json-validator?embed=1"
  width="100%"
  height="780"
  frameborder="0"
  style="border:0;border-radius:16px;max-width:900px;"
  title="JSON Validator by FixTools"
  loading="lazy"
  allow="clipboard-write"
></iframe>

Attribution-friendly: a small "Powered by FixTools" link appears in the embed footer.

Every Unexpected Token Error Type and Its Fix

The phrase Unexpected token covers many distinct root causes, and the character named in the message identifies which one applies. A closing brace or bracket means the parser hit a closing character immediately after a comma, which is the trailing comma signature: delete the comma. A comma means two commas appear in a row or a comma appears at the start of an array: remove the duplicate or the leading separator. A single quote means a developer wrote a string with the wrong quote character: replace single quotes with double quotes on strings and property names. A leading letter that begins the word undefined, the word NaN, or the word Infinity means a serialiser emitted a JavaScript literal that has no JSON equivalent: replace the value with null or with an appropriate placeholder.

Other token types point to different categories of problem. A less-than sign at position zero means the response is actually HTML, not JSON: the server returned an error page or a redirect, and the fix is on the server side or in the client request, not in the JSON itself. An opening parenthesis at position zero means the content is wrapped in a JSONP callback: strip the wrapper function call to recover the inner JSON. The text slash-slash or slash-star means a JavaScript comment was embedded in the JSON document: comments are not permitted in JSON, so remove them. The text end of input means a bracket, brace, or string was opened but never closed: count the openers and closers to find the imbalance.

The position in the error message is the single most diagnostic piece of information. Position zero almost always means the content is not JSON at all and you should investigate the source of the data rather than its syntax. Any other position points to a specific character in the body that broke the parse. That character either is wrong for its position, in which case you replace it, or it reveals that something earlier or later is wrong, in which case the fix may be a few characters away. FixTools converts the offset to a real line and column, categorises the token type, and explains the most likely cause so the path from error to fix is as short as the data permits.

After diagnosing and fixing an unexpected token error in the moment, the durable next step is to wire validation into a CI pipeline so the same class of failure cannot reach the main branch again. A typical GitHub Actions or GitLab CI job runs jq . or python3 -m json.tool over every JSON file changed in a pull request and exits with a non-zero status on the first failure, blocking the merge until the issue is fixed. Pre-commit hooks using the check-json hook from the standard pre-commit framework catch the failure on the developer machine before the commit reaches the remote at all. For runtime JSON arriving over the wire from external systems, a validation middleware catches parse failures, logs the raw input alongside the error message, and returns a structured 400 to the client. Layering pre-commit, CI, and runtime validation produces a defence in depth that turns unexpected token errors from recurring debugging cost into invisible plumbing the team never has to think about again.

How to use this tool

💡

Paste the JSON that is giving you an Unexpected token error. FixTools identifies the exact token, explains why it is invalid, and tells you what to fix.

How It Works

Step-by-step guide to fix json unexpected token:

  1. 1

    Paste the failing JSON

    Copy the exact JSON string that produced the Unexpected token error from your application logs, your terminal, or your editor. Paste the full content into the FixTools input area so the validator can scan the entire structure. Include any leading or trailing whitespace that was in the original because the validator needs the same bytes the parser saw to reproduce the failure faithfully.

  2. 2

    Run validation

    Click the Validate button to run the check. The validator reports three pieces of information: the line and column where parsing failed, the unexpected character that was found at that position, and a description of the most likely cause. The combination is enough to determine the fix without further investigation in the majority of cases, which is the goal of the tool.

  3. 3

    Identify the token type

    Look at the unexpected character that the validator named. A closing brace or bracket indicates a trailing comma immediately before it. A single quote indicates a wrong quote style. A leading letter such as u or N indicates a JavaScript literal that does not exist in JSON. A less-than sign at position zero indicates HTML rather than JSON. Each token type has a different fix, and recognising the type is the key step before editing.

  4. 4

    Apply the fix

    Edit the source at the exact location the validator reported. For a trailing comma, delete the comma before the bracket. For wrong quotes, replace the single quotes with double quotes. For a JavaScript literal, replace it with null or with an appropriate value. For an HTML response, investigate why the server returned the wrong content type. Save the file and prepare to revalidate to confirm the fix worked and no other errors were waiting behind it.

  5. 5

    Revalidate

    Paste the updated content into FixTools and run validation again. JSON parsers fail fast, so the first fix may unveil further errors that were hidden until the first one was resolved. Repeat the fix-and-revalidate cycle, addressing each reported error one at a time in order, until the validator returns a green result indicating the entire document conforms to RFC 8259.

Real-world examples

Common situations where this approach makes a real difference:

API developer

A developer integrates with a third-party API and starts seeing SyntaxError: Unexpected token T at runtime in production. They capture a failing response and paste it into FixTools. The validator highlights the literal text True with a capital T at line 34 in a boolean field. The API is serialising Python booleans directly, using the Python convention of capitalised first letters, rather than the JSON lowercase form. The developer adds a normalisation step that lowercases known Python literals before parsing, while filing a bug with the vendor to fix the serialiser at its source.

Full-stack developer

A developer writes a template that generates JSON config files from user input. One field contains a multi-line description that includes literal newline characters. The config loader rejects the file with Unexpected token at line eight column 23. FixTools highlights an unescaped newline inside a string value. The template was concatenating raw text into the JSON instead of running it through JSON.stringify, which would have produced the proper escape sequence. Switching the template to use JSON.stringify for value content fixes the issue and any future special characters.

Data engineer

A data export pipeline produces JSON files with numeric fields set to the literal text NaN where sensors failed to report. Downstream systems reject the files with Unexpected token N. The engineer adds a post-processing step that walks the structure and replaces NaN, Infinity, and negative Infinity with null in any numeric field before writing. They validate a representative sample in FixTools to confirm the replacement makes the files parseable, then deploy the fix to production and reprocess the backlog of rejected files.

Student developer

A student is writing their first API client in Node.js and hand-writes a test request body in a text file using Python conventions for booleans because their previous course was in Python. Their test client throws SyntaxError: Unexpected token T. They paste the body into FixTools and immediately see the capitalised True flagged with an explanation that JSON booleans must be lowercase. They fix the file, learn the rule, and move on. The visual highlighting and plain-language explanation made the rule stick faster than reading the JSON specification ever could.

Pro tips

Get better results with these expert suggestions:

1

The token character is your primary clue

The character named in the Unexpected token error tells you the fix category immediately. A less-than sign means the response is HTML rather than JSON. A single quote means wrong quote style. The letter u means undefined was emitted as a literal. The letter N means NaN. A closing brace or bracket means a trailing comma sits just before it. Memorising these mappings lets you guess the fix category before you even paste into the validator, which saves time on routine cases.

2

Position zero errors are always content-type issues

When the parse error position is zero, the entire input is wrong rather than just one character. The first byte of the buffer is not a valid JSON start character. The fix is not to look for a syntax error somewhere in the document but to investigate why the content is wrong: a 404 page from the server, a redirect to a login page, a JSONP wrapper, a BOM byte from a UTF-8-with-BOM file, or a Content-Type mismatch. Inspect the source of the data rather than its syntax.

3

End of input errors mean unclosed structures

The message Unexpected end of input or Unexpected end of JSON input signals that a brace, bracket, or string was opened during parsing and was never closed before the buffer ran out. Count the open braces against the close braces in your document, and the open brackets against the close brackets. The structure with the unmatched opener is the one missing its closer. A formatter that re-indents the document by nesting level usually reveals the imbalance visually.

4

Check keyword capitalisation for non-JavaScript languages

JSON output produced by Python serialisers may contain True, False, or None instead of the JSON lowercase true, false, and null. Ruby produces nil where JSON expects null. Java sometimes produces Boolean.TRUE instead of the lowercase literal when toString is overridden. Swift produces nil. Each language has its own conventions, and JSON serialisers in each language can sometimes leak the source convention into the output. Validate JSON that crosses language boundaries rather than trusting the producer to handle the edge cases correctly.

FAQ

Frequently asked questions

An Unexpected token error means the parser hit a character at a position where the grammar rule it was applying did not allow that character. Common causes include trailing commas before a closing brace or bracket, single quotes instead of double quotes around strings and property names, the literal text undefined or NaN where a value should be, a less-than sign at the start of the buffer meaning the content is HTML, a comment marker such as slash-slash or slash-star inside the document, and unescaped control characters inside string values. The named token character points to the specific cause.
The letter u at position zero usually means JSON.parse was called with the literal text undefined as input, because the function coerces non-string arguments to strings using ToString and the result for the JavaScript undefined value is the text undefined, which is not a JSON value. The fix is to check the type of the variable before parsing. Use typeof value equals string to confirm the input is actually a string and not the undefined or null value that someone forgot to populate. Return early with an appropriate default when the type check fails.
The less-than character at position zero is the opening angle bracket of an HTML document, typically the doctype declaration or the html start tag. The server returned an HTML response rather than the JSON the client expected. The most common causes are a 404 Not Found page, a 401 Unauthorized redirect to a login page, a 500 Internal Server Error page, or a maintenance status page. Check the HTTP status code and the Content-Type header in the response. The fix is on the request side or the server side, not in the JSON structure.
Replace every single-quoted string and property name with the double-quoted equivalent. The JavaScript object that uses single quotes everywhere becomes the JSON object that uses double quotes everywhere. In a large file, use your editor's find-and-replace with care because a single quote character can legitimately appear inside a double-quoted string value as data and must not be changed. Focus the search on single quotes that appear at the boundaries of string and property name positions, and step through matches manually to preview each replacement.
The JavaScript undefined value has no JSON representation. JSON.stringify silently omits properties whose value is undefined when it serialises an object, which is the safe behaviour. Some hand-written serialisers or older libraries emit the literal text undefined into the output instead of omitting the field, which produces invalid JSON. The parser sees the u of undefined and reports Unexpected token u. The fix is to replace undefined with null before serialising, or to use a custom replacer function with JSON.stringify that converts undefined to null explicitly.
The message means the input buffer ended while the parser was still inside an incomplete structure. A brace was opened but never closed, a bracket was opened but never closed, or a string was opened with a double quote but never closed with another double quote. The structure expected more characters than the buffer contained. Find the unmatched opener by counting opens against closes, or by running the document through a formatter that re-indents by nesting level so the unclosed structure visibly cascades to the wrong indentation.
There is no universal automatic fixer because the right repair depends on the human intent behind the data. A trailing comma might mean the last element should be removed or that a new element was about to be added. An undefined value might mean null was intended or that the field should be dropped entirely. A single quote might mean every string in the document should be converted or that just one was mistyped. FixTools identifies the exact location and category of each error so a developer can apply the intent-aware fix in seconds without guessing.
The capital N usually marks the start of the literal text NaN, the JavaScript floating-point Not a Number value. NaN has no JSON equivalent because the JSON specification requires numbers to be finite IEEE 754 values. Some serialisers emit NaN literally into the JSON stream where the source object contained a NaN value. The fix is to replace the value with null, with zero, or with an appropriate placeholder before serialising. The same fix applies when the unexpected token is I from Infinity or hyphen-I from negative Infinity.
When the engine message alone is not enough to pinpoint the cause, layer additional tools on top of the raw output. A hex viewer such as xxd or the built-in hex mode in VS Code reveals invisible characters, BOM bytes, and encoding issues that look identical to valid characters in a regular editor view. A diff tool comparing the failing document against a known-good template highlights exactly which bytes diverge between the two. A JSON formatter that indents the document by nesting level visually exposes structural imbalance because subsequent content sits at the wrong indent depth when a bracket is missing or extra. For programmatic debugging in JavaScript, the json-source-map package returns line and column information for every node in the parsed tree, which lets you write tooling that points at specific paths in the original source. Combining these views with FixTools resolves even the hardest unexpected token errors within minutes of focused investigation.

Ready to get started?

Open the full JSON Validator — free, no account needed, works on any device.

Open JSON Validator →

Free · No account needed · Works on any device