How to Validate JSON in JavaScript

5 min read

JSON shows up everywhere in JavaScript — API responses, config files, `localStorage` entries — and almost all of it arrives as a plain string that has to be parsed before your code can use it. "Validating" JSON just means confirming that string is actually well-formed JSON before you trust it, and JavaScript gives you the tools to do that without any library.

The one function that matters: JSON.parse

JSON.parse() is both the fastest and the most reliable way to check JSON in JavaScript — if it doesn't throw, the string was valid JSON. If it does throw, the input wasn't valid, and the error tells you why:

function isValidJson(input) {
  try {
    JSON.parse(input);
    return true;
  } catch (error) {
    return false;
  }
}

isValidJson('{"name": "Alice"}'); // true
isValidJson('{name: "Alice"}');   // false — unquoted key

Getting a useful error message, not just true/false

A boolean is enough for a guard clause, but when you're debugging a broken payload you want to know where it broke. The error object thrown by JSON.parse includes a message like Unexpected token o in JSON at position 14 — the position is a character offset into the string, not a line/column, so for anything longer than a one-liner you'll want to convert it yourself:

function describeJsonError(input, error) {
  const match = error.message.match(/position (\d+)/);
  if (!match) return error.message;

  const position = Number(match[1]);
  const before = input.slice(0, position);
  const line = before.split('\n').length;
  const column = position - before.lastIndexOf('\n');

  return `${error.message} (line ${line}, column ${column})`;
}

This is exactly the gap our JSON Validator tool fills — it does this conversion for you and points at the exact line and column, so you don't have to write or maintain this helper yourself.

Common reasons JSON.parse throws

  • Trailing commas{"a": 1, "b": 2,} is valid in a JavaScript object literal but not in JSON.
  • Single-quoted strings — JSON requires double quotes; JavaScript object literals don't care.
  • Unquoted keys{name: "Alice"} is valid JS, not valid JSON; keys must be quoted strings.
  • Comments — JSON has no comment syntax at all, unlike JSON5 or JSONC (common in config files like tsconfig.json).
  • A trailing or leading non-JSON character — a stray newline is fine, but an extra }, a BOM character, or accidentally including surrounding backticks from a template literal will all fail.

Validating without throwing on every keystroke

If you're validating JSON as a user types (an editor, a form field), wrapping every keystroke in try/catch works but generates a lot of exceptions for what's usually mid-typing, temporarily-invalid input. It's fine performance-wise — exceptions in modern JS engines are cheap when caught locally — but debounce the check (a few hundred milliseconds) so you're not surfacing an error banner while someone is still typing an opening bracket.

What JSON.parse won't tell you

Syntax validity isn't the same as shape validity. JSON.parse will happily accept {"user": null} even if your code expects user to always be an object — that's a schema problem, not a parsing problem, and needs a separate validation layer (like Zod or JSON Schema) if you need to guarantee structure, not just syntax.

Try JSON Validator now

Check JSON validity with precise line/column errors.

Open JSON Validator