Fix Invalid JSON Errors in Node.js

"SyntaxError: Unexpected token in JSON" is one of the most common runtime errors Node.js developers hit when parsing data from an API, a config file, or a request body. This guide explains exactly what causes this error, how to read the error message to find the broken character, and how to prevent it from crashing your application.

What the error actually means

JSON.parse() throws a SyntaxError whenever the string you pass it is not valid JSON according to the JSON specification. A typical error looks like this:

SyntaxError: Unexpected token } in JSON at position 42
    at JSON.parse (<anonymous>)

The position number tells you the character index where parsing failed — you can use it to locate the exact problem in the raw string.

Common cause 1: Trailing commas

Unlike JavaScript object literals, strict JSON does not allow a trailing comma after the last property:

// Invalid JSON — trailing comma
'{"name": "Rahul", "age": 25,}'

// Valid JSON
'{"name": "Rahul", "age": 25}'

Common cause 2: Single quotes and unquoted keys

JSON requires double quotes for both keys and string values. Single quotes, which are valid in JavaScript object literals, are not valid JSON:

// Invalid JSON
"{'name': 'Rahul', age: 25}"

// Valid JSON
'{"name": "Rahul", "age": 25}'

Common cause 3: Parsing an empty or undefined value

This is one of the most frequent causes in Express APIs — parsing an empty request body or an undefined variable that was never converted to a string:

JSON.parse('');        // SyntaxError: Unexpected end of JSON input
JSON.parse(undefined); // SyntaxError: "undefined" is not valid JSON

Fix: always wrap JSON.parse in try/catch

The most important defensive pattern in Node.js is to never call JSON.parse without handling the potential SyntaxError:

function safeJsonParse(input) {
  try {
    return { data: JSON.parse(input), error: null };
  } catch (err) {
    return { data: null, error: err.message };
  }
}

const result = safeJsonParse(rawString);
if (result.error) {
  console.error('Invalid JSON received:', result.error);
} else {
  console.log(result.data);
}

In an Express app, add an error-handling middleware right after express.json() to catch body-parsing failures gracefully instead of letting them crash the request:

app.use(express.json());

app.use((err, req, res, next) => {
  if (err.type === 'entity.parse.failed') {
    return res.status(400).json({ error: 'Invalid JSON in request body' });
  }
  next(err);
});

Checklist for debugging invalid JSON

  • Log the raw string before calling JSON.parse to see exactly what was received.
  • Check for trailing commas, single quotes, and unquoted keys.
  • Confirm the string is not empty, undefined, or already an object being parsed twice.
  • Run the string through a JSON formatter tool to pinpoint the exact broken character.
  • For APIs, verify the Content-Type header matches the actual body format being sent.

Frequently Asked Questions

Why does Node.js throw SyntaxError: Unexpected token in JSON?

This error means JSON.parse received a string that is not valid JSON — common causes include trailing commas, single quotes instead of double quotes, unquoted keys, or an empty string being parsed.

How do I safely parse JSON in Node.js without crashing the app?

Wrap JSON.parse in a try/catch block and handle the SyntaxError gracefully, returning a clear error response instead of letting the exception crash the process.

Why does my Express app throw a JSON parsing error on POST requests?

This usually happens when the request body is empty or malformed but the Content-Type header is still set to application/json, causing the express.json() middleware to fail while parsing.

Try the Free JSON Formatter

Paste your broken JSON and instantly see exactly where the syntax error is, with formatting and validation.

Related articles