THEJORD LogoTHEJORD

How to Validate JSON Effectively: Complete Guide

THEJORD Teamโ€ขโ€ข5 min read
jsonvalidationdevelopmenttools

Complete guide to JSON validation: online tools, libraries, JSON Schema and best practices.

How to Validate JSON Effectively: Complete Guide

It's 11:47 PM, production deploy is in 15 minutes, and your API returns a mysterious 500 Internal Server Error. After half an hour of debugging, you find the culprit: an extra comma in a JSON configuration file. Sound familiar? You're not aloneโ€”JSON errors are among the most frequent and frustrating bugs in web development.

In this guide, we'll cover how to validate JSON effectively, recognize common errors at a glance, and set up a workflow that saves you from sleepless nights.

Why JSON betrays you (when you least expect it)

JSON seems simple: curly braces, quotes, commas. But its simplicity is deceptive. Unlike JavaScript, JSON has strict rules that don't forgive. A single misplaced character and the entire document becomes unreadable.

The problem gets worse when working with:

  • Configuration files (package.json, tsconfig.json, settings.json)
  • API responses from external services
  • Dynamically generated data from backends or scripts
  • Import/export between different systems

In all these cases, malformed JSON doesn't generate a clear error: you often get cryptic messages like "Unexpected token" or, worse, a silent failure.

The 6 JSON errors everyone makes (and how to spot them)

1. The trailing comma trap

The most common error by far. JavaScript tolerates them, JSON doesn't.

// WRONG - comma after the last element
{
  "firstName": "John",
  "lastName": "Doe",  // โ† this comma breaks everything
}

// CORRECT
{
  "firstName": "John",
  "lastName": "Doe"
}

How to spot it: error "Unexpected token }" - the parser finds the closing brace when expecting another value.

2. Wrong quotes

JSON only accepts double quotes. No single quotes, no backticks.

// WRONG
{ 'name': 'John' }
{ `name`: `John` }

// CORRECT
{ "name": "John" }

How to spot it: error on the first line, usually "Unexpected token '".

3. Missing comma between elements

Easy to introduce when quickly adding properties.

// WRONG - missing comma
{
  "firstName": "John"
  "lastName": "Doe"
}

// CORRECT
{
  "firstName": "John",
  "lastName": "Doe"
}

How to spot it: "Unexpected string" pointing to an apparently correct line. Check the previous line.

4. Unescaped characters

Quotes and backslashes inside strings must be escaped.

// WRONG
{ "message": "He said "hello"" }
{ "path": "C:\Users\John" }

// CORRECT
{ "message": "He said \"hello\"" }
{ "path": "C:\\Users\\John" }

5. Unquoted values

Numbers and booleans go without quotes, but strings always need them.

// WRONG
{ "name": John, "active": "true" }

// CORRECT
{ "name": "John", "active": true }

6. Comments (which don't exist)

JSON doesn't support comments. Neither // nor /* */.

// WRONG
{
  "debug": true  // development mode
}

// CORRECT (remove the comment)
{
  "debug": true
}

Workaround: use a dedicated property like "_comment": "explanation" or switch to JSONC (JSON with Comments) for local configuration files.

4 ways to validate JSON (from fastest to most robust)

1. Browser console (30 seconds)

The quickest method for a fast check. Open DevTools (F12), go to Console and type:

JSON.parse('{"name": "John"}')
// If it returns the object โ†’ valid JSON
// If it throws an error โ†’ malformed JSON

The error message also indicates the approximate position of the problem.

2. Online tools (1 minute)

For larger files or when you also want formatting. Tools like JSON Formatter show you:

  • Syntax errors with line numbers
  • Automatic formatting (correct indentation)
  • Structure highlighting

3. IDE extensions (one-time setup)

For continuous validation while you code:

  • VS Code: native JSON validation, just save as .json
  • ESLint + plugins: to validate JSON in configuration files
  • Prettier: auto-formats and flags errors

In VS Code, press Ctrl+Shift+M to see all problems in open files.

4. Programmatic validation (for applications)

When receiving JSON from external sources, always validate before using:

// JavaScript/TypeScript
function parseJSON(str) {
  try {
    return { success: true, data: JSON.parse(str) };
  } catch (e) {
    return { success: false, error: e.message };
  }
}

// Usage
const result = parseJSON(responseBody);
if (!result.success) {
  console.error("Invalid JSON:", result.error);
}

For advanced validation (checking that data has the expected structure), use JSON Schema with libraries like Ajv or Zod. For quick tests, try our online JSON validator.

Best practices to avoid JSON errors

  1. Don't write JSON by hand when possible. Use JSON.stringify() to generate it from objects.
  2. Configure auto-formatting in your editor. Well-indented JSON reveals errors at a glance.
  3. Validate in CI/CD: add an automatic check in your pipelines for JSON configuration files.
  4. Use TypeScript or JSON Schema to define the expected data structure.
  5. For config files, consider alternatives that support comments: JSONC, YAML, or TOML.

Conclusion

JSON validation isn't glamorous, but it's one of those skills that separates efficient developers from those who spend hours debugging. Now you have the tools to:

  • Recognize the 6 most common errors at a glance
  • Validate JSON in 30 seconds with the console
  • Set up a workflow that prevents errors at the source

Try our free JSON Formatter now: paste your JSON, get instant validation and clean formatting.