Skip to content

Search WPDesignVault

Try "image", "json", "schema" or "contrast". Use arrow keys to move through results.

Developer Guides

How to Read and Fix JSON Syntax Errors

JSON looks simple until a single misplaced comma breaks an import, an API request or a configuration file. The good news is that almost every JSON error comes from a short list…

Comprehensive guide and visual tutorial explaining fix json syntax errors for WordPress websites.
On this page

JSON looks simple until a single misplaced comma breaks an import, an API request or a configuration file. The good news is that almost every JSON error comes from a short list of causes. This guide shows how to read parser error messages and fix the mistakes that cause them.

The rules JSON actually enforces

  • Strings and object keys use double quotes. Single quotes are invalid.
  • No trailing commas after the last item in an object or array.
  • No comments. Neither // nor /* */ is allowed.
  • Values are only strings, numbers, true, false, null, objects and arrays. No undefined, NaN, Infinity or functions.
  • Numbers have no leading zeros (007 is invalid) and no leading plus sign.
  • Special characters inside strings must be escaped: ” for a quote, \ for a backslash, n for a newline.
<h2
Step-by-step workflow architecture diagram for implementing fix json syntax errors on live sites.
Three-phase workflow architecture diagram for generating, deploying, and testing fix json syntax errors.

Reading the error message

Parsers report where they gave up, which is often just after the real mistake. Different tools word it differently:

MessageUsually means
Unexpected token } in JSON at position 52A trailing comma before the closing brace.
Unexpected token ‘ in JSONSingle quotes instead of double quotes.
Expected property name or ‘}’An unquoted key, or a trailing comma in an object.
Unexpected end of JSON inputThe text was cut off: a missing closing bracket or brace.
Unexpected token u in JSON at position 0You parsed the string “undefined”, typically an empty variable in JavaScript.
Unexpected token < in JSON at position 0The server returned HTML (often an error page) instead of JSON.
Bad control character in string literalA raw line break or tab inside a string; escape it as n or t.

Paste the JSON into the JSON validator; it converts “position” into a line and column and highlights the spot.

The five fixes that solve most errors

1. Remove the trailing comma

{ "name": "WPDV", "tags": ["seo", "tools",] }   ✗
{ "name": "WPDV", "tags": ["seo", "tools"] }    ✓

2. Use double quotes everywhere

{ 'title': 'Hello' }   ✗
{ "title": "Hello" }   ✓

3. Quote every key

{ title: "Hello" }     ✗  (valid JavaScript, invalid JSON)
{ "title": "Hello" }   ✓

4. Escape quotes and backslashes inside strings

{ "html": "<a href="/">Home</a>" }     ✗
{ "html": "<a href="/">Home</a>" }   ✓
{ "path": "C:Usersme" }               ✗
{ "path": "C:\Users\me" }             ✓

5. Close every bracket

Count opening and closing braces and brackets, or format the JSON so the indentation shows where a block is not closed. The JSON formatter indents valid JSON and points at the line where structure breaks.

<h2
Recommended best practices checklist and common pitfalls to avoid for fix json syntax errors.
Implementation checklist comparing recommended best practices against common errors in fix json syntax errors.

Invisible problems

  • Smart quotes. Word processors and some chat apps turn ” into “ ”. Retype them in a code editor.
  • A byte order mark (BOM) at the start of a file saved by some Windows editors. Save as “UTF-8 without BOM”.
  • Non-breaking spaces copied from web pages. They look like spaces but are not whitespace to a JSON parser.
  • Duplicate keys. Technically allowed by the spec, but most parsers silently keep only the last one, which causes confusing bugs.

When the JSON comes from a server

If JSON.parse fails on an API response, look at the raw response first (the Network tab in your browser’s developer tools). An HTML error page, a PHP warning printed before the JSON, or a redirect to a login page all produce “Unexpected token <“. Fix the server error rather than the parser.

In WordPress, a common cause is a plugin or theme printing a notice. Turn on WP_DEBUG_LOG and turn off WP_DEBUG_DISPLAY so warnings go to the log, not into REST and AJAX responses.

Need comments? Use another format

If a configuration file needs comments, JSON is the wrong tool. YAML supports comments and is easier to read, and many tools accept it. The YAML ⇄ JSON converter converts between the two, with line-numbered errors on both sides.

Checklist

  • Double quotes only, on keys and strings.
  • No trailing commas, no comments.
  • Escape ” and \ inside strings.
  • Every { and [ has a matching } and ].
  • Check the raw response when parsing server output.

JSON Validator

Check whether JSON is valid and find the exact line and column of any syntax error.

Reorder, rotate and extract PDF pages
Developer Guides

Reorder, Rotate and Extract PDF Pages

Reordering, rotating, extracting and splitting each change a PDF differently. This guide explains when to use each, how to do it in…

4 min read

Comments

Leave a Reply

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