How to validate JSON online: a developer guide with practical examples

8 min read

Learn how to validate, format, and debug JSON easily. Common errors, JSON vs XML comparison, best practices, and a free online tool.

What is JSON and why you need to validate it

JSON (JavaScript Object Notation) is the most widely used data interchange format in modern web development. Virtually every REST API returns data in JSON, tool configurations like package.json and tsconfig.json use this format, and databases like MongoDB store documents in JSON format.

The problem is that a single misplaced character (an extra comma, a missing quote, an unclosed bracket) makes the entire JSON invalid and causes errors in your application. Invalid JSON in an API response can break your frontend. A tsconfig.json with a syntax error prevents TypeScript from compiling.

Validating JSON before using it prevents hours of debugging. With the NexTools JSON validator, you can paste any JSON, see errors with the exact line number where they occur, and get the prettified format automatically.

JSON syntax basics: rules you must know

JSON has strict syntax rules. Breaking any of them produces invalid JSON:

  • Keys must be in double quotes: {"name": "Ana"} is valid. {name: "Ana"} is NOT.
  • Strings use double quotes, never single: "value" is valid. 'value' is NOT.
  • No trailing commas: {"a": 1, "b": 2} is valid. {"a": 1, "b": 2,} is NOT.
  • No comments: You cannot use // or /* */ inside JSON. For configs that need comments, use JSONC or JSON5.
  • Numbers are not quoted: {"age": 25} is valid. {"age": "25"} works but the value is a string, not a number.

Allowed data types in JSON:

TypeExampleNotes
String"hello world"Always double quotes
Number42, 3.14, -7Integers or decimals, no quotes
Booleantrue, falseLowercase, no quotes
NullnullLowercase, no quotes
Array[1, 2, 3]Comma-separated values
Object{"key": "value"}Key-value pairs

The 10 most common JSON errors

These are the errors you will encounter most frequently when working with JSON:

  1. Trailing comma on the last element:
    {"items": [1, 2, 3,]} — The comma after 3 is invalid.
  2. Single quotes instead of double:
    {'name': 'John'} — Must be {"name": "John"}.
  3. Unquoted keys:
    {age: 30} — Must be {"age": 30}.
  4. Unescaped control characters:
    Literal tabs and newlines inside strings must be written as \t and \n.
  5. Unescaped quotes inside strings:
    {"text": "he said "hello""} — Must be {"text": "he said \"hello\""}.
  6. Comments:
    JSON does not support comments of any kind.
  7. NaN, Infinity, or undefined:
    These JavaScript values are NOT valid in JSON. Use null instead.
  8. Hexadecimal values:
    {"color": 0xFF0000} is not valid. Use {"color": "#FF0000"} or {"color": 16711680}.
  9. Mixed types in typed arrays:
    [1, "two", true] is valid JSON, but many APIs expect homogeneous arrays.
  10. BOM (Byte Order Mark):
    Some editors add invisible bytes at the start of the file that invalidate JSON. Always save as UTF-8 without BOM.

All of these errors are detected automatically by the JSON validator, which shows you the exact line and position of each error.

How to format and minify JSON

JSON can be presented in two ways: pretty-printed for human readability, and minified for efficient transmission.

Pretty-printed JSON:

{
  "user": {
    "name": "Maria",
    "age": 28,
    "active": true
  }
}

Minified JSON (same data, fewer bytes):

{"user":{"name":"Maria","age":28,"active":true}}

The minified version takes up less space (ideal for APIs and storage), while the pretty-printed version is much easier to read and debug. In the example above, minified JSON is 48 bytes vs 75 bytes prettified — a 36% reduction.

In JavaScript, you can format JSON with:

JSON.stringify(data, null, 2)  // pretty with 2 spaces
JSON.stringify(data)           // minified

For minifying large JSON files, use the code minifier which handles JSON, CSS, and JavaScript.

JSON vs XML: when to use each

Before JSON, XML was the dominant data interchange format. Both are still in use, but for different purposes:

FeatureJSONXML
ReadabilityMore conciseMore verbose
Size30-50% smallerLarger
Data typesString, number, boolean, null, array, objectEverything is text
CommentsNot supportedSupported
SchemasJSON SchemaXSD, DTD
Primary useREST APIs, configurationSOAP, documents, RSS
ParsingNative JSON.parse()Requires XML parser

In 2026, JSON dominates in web development, APIs, and mobile applications. XML remains in legacy enterprise systems, RSS/Atom feeds, and document formats like DOCX (which is internally compressed XML).

Tools for working with JSON every day

Besides validating JSON, these complementary tools make your workflow easier:

  • JSON Validator: Checks syntax, shows errors with line numbers, and auto-formats.
  • Code Minifier: Compresses JSON by removing whitespace and line breaks.
  • Base64 Encoder: Encodes JSON to Base64 for transmission in URLs or HTTP headers.
  • Text Comparator: Compare two JSON versions to find differences.

In the browser:

  • JSON.parse(jsonString) — Converts a JSON string to a JavaScript object. Throws an error if the JSON is invalid.
  • JSON.stringify(obj, null, 2) — Converts an object to formatted JSON.
  • console.dir(JSON.parse(json)) — Displays the JSON structure interactively in the console.

JSON Schema: validating structure, not just syntax

Syntax validation only checks that JSON is well-formed. But in real applications, you also need to verify that data has the correct structure (required fields exist, types are correct, values fall within expected ranges).

This is what JSON Schema is for — a standard for describing the structure of JSON documents:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["name", "age", "email"],
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "age": { "type": "integer", "minimum": 0, "maximum": 150 },
    "email": { "type": "string", "format": "email" }
  }
}

With this schema, you can automatically validate that any user JSON has a name (non-empty string), age (integer between 0 and 150), and email (valid format). Libraries like Ajv (JavaScript), jsonschema (Python), and Newtonsoft (.NET) implement JSON Schema validation.

In professional development, defining JSON Schemas for your APIs is one of the best practices for preventing errors and automatically documenting your API.

Best practices for working with JSON in production

These practices will help you avoid common issues in real projects:

  • Always validate JSON from external sources: Any JSON you receive from an API, user-uploaded file, or webhook should go through JSON.parse() inside a try-catch before processing.
  • Use camelCase for keys: This is the standard convention in JavaScript/TypeScript. userName instead of user_name or UserName.
  • Avoid nesting more than 3-4 levels: Deeply nested JSON is hard to read, traverse, and maintain. Flatten the structure when possible.
  • Never store sensitive data in unencrypted JSON: Tokens, passwords, and private keys should never appear in plain JSON in logs or API responses.
  • Use null instead of omitting optional fields: {"middleName": null} is better than omitting the field, because the API consumer knows explicitly that the field exists but has no value.
  • Handle parse errors gracefully: Do not show the raw JSON.parse error to end users. Log it internally and display a friendly message.

Try this tool:

Open tool

Frequently asked questions

Why does my JSON show 'Unexpected token' when parsing

The 'Unexpected token' error means JSON.parse found an unexpected character. The most common causes are: single quotes instead of double, a trailing comma on the last element, comments in the JSON, or invisible BOM characters at the start of the file. Paste your JSON into an online validator to see exactly where the error is.

Can I use comments inside a JSON file

No. The JSON specification does not allow comments of any kind (neither // nor /* */). If you need JSON with comments, you can use alternative formats like JSONC (JSON with Comments, used by VS Code in settings.json), JSON5, or YAML. Another option is using a special field like '_comment' as a key.

What is the difference between JSON and a JavaScript object

A JavaScript object can have unquoted keys, functions as values, undefined, Symbol, and trailing commas. JSON is more restrictive: all keys must have double quotes, only allows string, number, boolean, null, array and object as values, and does not allow trailing commas or comments. JSON is a subset of JavaScript object syntax.

How much JSON is too large to process in the browser

Most modern browsers can parse JSON up to 50-100 MB without issues. Beyond that, parsing can block the main thread and freeze the interface. For very large JSON, use streaming parsers like JSONStream or process the data server-side. As a reference, a JSON with 100,000 simple objects weighs approximately 10-15 MB.

How can I convert JSON to CSV or vice versa

For JSON to CSV: if the JSON is an array of objects with the same keys, the keys become columns and each object becomes a row. For CSV to JSON: each row becomes an object with the headers as keys. You can do this programmatically with JavaScript (using map/reduce) or with online tools like NexTools.

Is JSON better than YAML for configuration files

It depends on the use case. JSON is stricter (fewer ambiguous errors), has faster parsing, and is the standard for APIs. YAML is more human-readable, supports comments, and is popular in DevOps (Docker Compose, GitHub Actions, Kubernetes). For application configs that humans will edit, YAML is often better. For data transmitted between machines, JSON is the standard choice.