Home/Blog/How to Validate JSON in JavaScript
JSON · JavaScript

How to Validate JSON in JavaScript

4 min readUpdated January 2025Developer Tools
Validating JSON in JavaScript is something every developer needs at some point — API responses, user input, config files. Here are the three most reliable methods with copy-paste examples.

JSON Formatter & Validator

Paste JSON and validate it instantly — errors highlighted with line numbers.

Open JSON Formatter & Validator →

Why validate JSON in JavaScript?

JSON.parse() throws a SyntaxError on invalid JSON, which will crash your app if unhandled. Always validate before parsing user-supplied or external JSON.

Method 1 — try/catch (simplest)

The most common approach. Wrap JSON.parse() in try/catch to safely handle invalid input:

JavaScript
function isValidJSON(str) {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
}

// With the parsed result and error details
function parseJSON(str) {
  try {
    return { ok: true, data: JSON.parse(str), error: null };
  } catch (e) {
    return { ok: false, data: null, error: e.message };
  }
}

const result = parseJSON('{"name":"Ada"}');
if (result.ok) {
  console.log(result.data.name); // "Ada"
} else {
  console.error(result.error);
}

Method 2 — validate type after parsing

JSON.parse() accepts primitives like "true" or 42 as valid JSON — but you usually expect an object or array. Add a type check:

JavaScript
function isValidJSONObject(str) {
  try {
    const parsed = JSON.parse(str);
    return typeof parsed === 'object' && parsed !== null;
  } catch (e) {
    return false;
  }
}

function isValidJSONArray(str) {
  try {
    const parsed = JSON.parse(str);
    return Array.isArray(parsed);
  } catch (e) {
    return false;
  }
}

Method 3 — JSON Schema validation

For validating that JSON has the right structure — correct fields, correct types — use a JSON Schema library like ajv:

Node.js (ajv)
import Ajv from 'ajv';
const ajv = new Ajv();

const schema = {
  type: 'object',
  required: ['id', 'name', 'email'],
  properties: {
    id:    { type: 'integer' },
    name:  { type: 'string', minLength: 1 },
    email: { type: 'string', format: 'email' }
  },
  additionalProperties: false
};

const validate = ajv.compile(schema);

const data = JSON.parse(input);
if (validate(data)) {
  console.log('Valid!');
} else {
  console.error(validate.errors);
}
Quick tip: For development and debugging, use the online JSON validator to instantly spot errors with line numbers — faster than running code.

Validating JSON in Node.js vs browser

The try/catch approach works identically in both environments. JSON Schema libraries like ajv work in both too. The only difference is how you read the input — from a file in Node.js, from user input or fetch response in the browser.

Node.js — validate a JSON file
import { readFileSync } from 'fs';

function validateJSONFile(path) {
  try {
    const content = readFileSync(path, 'utf8');
    JSON.parse(content);
    return { valid: true };
  } catch (e) {
    return { valid: false, error: e.message };
  }
}

Frequently asked questions

Does JSON.parse validate JSON completely?
JSON.parse validates syntax — missing brackets, bad quotes, trailing commas, and so on. It does not validate structure or data types. For structure validation, use JSON Schema with a library like ajv, zod, or yup.
What error does JSON.parse throw on invalid JSON?
It throws a SyntaxError with a message like "Unexpected token } in JSON at position 42". The message includes the position of the error, which you can extract to show users where their JSON is broken.
How do I validate JSON from a fetch response?
Use response.json() which internally calls JSON.parse. Wrap it in try/catch: try { const data = await res.json(); } catch(e) { /* invalid JSON */ }

Try it free — no sign-up needed

Runs entirely in your browser. Nothing uploaded, nothing stored.

Open JSON Formatter & Validator →

Related tools on tinybench.dev