Validate and lint JSON

What a JSON validator, linter and formatter actually change, when reformatting is safe, and how to keep JSON usable after copying or sharing it.

A JSON validator answers one question: does this text parse as JSON? A linter goes further and flags things that parse but may cause trouble, such as duplicate keys or inconsistent formatting. A formatter rewrites the text, either indenting it for people or minifying it for transport. These three jobs often share one tool, but they carry different risks. This review explains what each one changes, when the output keeps its meaning, and when you are better off leaving the original text alone.

A note on scope: no specific validator, website or editor plugin was tested for this article, and no measurements were taken. The guidance below comes from the JSON specification (RFC 8259 and ECMA-404) and from widely documented parser behavior. Check any particular tool against its own documentation before relying on it.

Is validating and linting JSON suitable for your use?

Validation is almost always worth doing. It changes nothing and reports whether the text is well-formed, so the only cost is the time it takes. If you edit configuration files, API payloads or test fixtures by hand, a syntax check catches missing commas and stray quotes before a program fails on them.

Reformatting is where tradeoffs appear. Pretty-printing and minifying preserve meaning in most cases, but a formatter that parses the data and serializes it again can quietly alter values the parser cannot represent exactly. The most important case is very large integers, covered below. It can also reorder keys or rewrite escape sequences. These changes are harmless under the specification but can matter to a consumer that depends on exact text.

Keep the original text unchanged in these situations:

  • The file is a JSON variant such as JSONC or JSON5 that allows comments or trailing commas. A strict validator will reject it, and a strict formatter may strip the comments.
  • The text is signed, hashed or compared byte for byte. Any whitespace or ordering change breaks the match.
  • The data contains integers beyond what the parsing environment can represent exactly, such as 64-bit IDs.
  • You are not sure the formatter preserves key order and a downstream consumer relies on it.

What validation, linting and formatting actually change

Validation changes nothing

A syntax validator reads the text and reports either success or the location of the first error, usually as a line and column. The text itself is not modified. The checks follow the specification. Strings and object keys must use double quotes. Trailing commas are not allowed. Comments are not part of JSON. Numbers cannot have leading zeros, and NaN, Infinity and hexadecimal forms are not allowed. Whitespace between tokens may only be space, tab, line feed or carriage return.

For example, the following text fails validation in two places: a trailing comma inside the array and a single-quoted key.

{
  "name": "widget",
  "tags": ["a", "b",],
  'size': 10
}

The corrected version is valid:

{
  "name": "widget",
  "tags": ["a", "b"],
  "size": 10
}

Syntax validation is different from schema validation. A syntax check confirms the text is JSON. A schema check, often written in JSON Schema, confirms the data has the expected fields and types. A document can pass the first check and still fail the second.

Linting reports problems and may suggest fixes

Linters flag issues that are syntactically legal but risky. The clearest example is a duplicate key. RFC 8259 says object names should be unique and warns that behavior is unpredictable when they are not. JavaScript's JSON.parse keeps the last value, so the input below becomes an object where "a" is 2. Other parsers may keep the first value or reject the input. A plain validator may accept this text, while a linter is more likely to flag it.

{"a": 1, "a": 2}

Style rules such as indentation width or sorted keys are also linting concerns. When a linter offers an automatic fix, that fix is a reformatting step and carries the risks described next.

Formatting rewrites whitespace, and sometimes more

Pretty-printing adds line breaks and indentation between tokens. Minifying removes them. Whitespace inside string values is part of the data and must not change. In this example, the space in "x y" survives both forms:

{"a":[1,2],"b":{"c":"x y"}}

{
  "a": [
    1,
    2
  ],
  "b": {
    "c": "x y"
  }
}

Many formatters work by parsing the input into data structures and serializing it again. Only insignificant whitespace should change, but a round trip can also alter:

  • Large numbers. Parsers that store numbers as IEEE 754 doubles, including JavaScript, represent integers exactly only up to 9007199254740991. The input 9007199254740993 parses as 9007199254740992 and is written back that way.
  • Number spelling. Values such as 1.0 or 1e2 may be written as 1 or 100. These are the same number but different text.
  • Escapes. A sequence like \u00e9 may come back as the literal character é, or the reverse. Both represent the same string.
  • Key order. The specification treats objects as unordered, so sorting keys does not change meaning under the spec. Software that reads keys in sequence, or compares files as text, may still see a difference.
  • Duplicate keys. A parse-and-reserialize step typically collapses them to a single key, silently dropping one of the values.

Check a formatter's documentation to see whether it works on tokens, which preserves the original text more faithfully, or on parsed values, which exposes it to the changes above. This was not verified for any specific tool in this review.

Operation | What changes | Meaning preserved?
Syntax validation | Nothing | Yes
Linting (report only) | Nothing | Yes
Pretty-print or minify | Whitespace between tokens | Yes, if the tool only touches whitespace
Parse and reserialize | Whitespace, number spelling, escapes, possibly key order | Usually; not for oversized integers or duplicate keys
Key sorting | Order of object members | Yes under the spec; not for order-dependent consumers

Will the JSON stay readable and usable after copying or sharing?

Many JSON failures come from the trip between applications rather than from the data. Common problems introduced by copying and pasting include:

  • Curly quotes. Word processors, some chat apps and some email clients replace straight double quotes with typographic quotes. JSON accepts only the straight double quote.
  • Non-breaking spaces. Copying from web pages or rich-text documents can introduce U+00A0, which JSON does not accept as whitespace between tokens.
  • Invisible characters. Zero-width spaces or a byte order mark at the start can cause a parse error that is hard to see. RFC 8259 says implementations must not add a byte order mark to JSON text, though parsers may ignore one.
  • Wrapped lines. Some destinations break long lines. This is harmless between tokens but corrupts a string value if the break lands inside it.

When a validator reports an error at a spot that looks correct, suspect one of these characters. An editor that can show invisible characters or code points will help you find it.

Readability depends on form. Pretty-printed JSON shows structure through indentation, and validator errors point to a meaningful line. Minified JSON is compact but hard to read or edit, and an error at line 1, column several thousand is difficult to locate. Keep minified output for transport and storage, and pretty-print anything a person will review.

Accessibility was not tested for this article. Indentation conveys nesting visually, but how a given screen reader announces indentation, brackets and long single-line JSON depends on the software and its settings. Check the screen reader's documentation or test with it directly. A short plain-language summary of the structure alongside the JSON is a safe addition when the audience may include assistive technology users.

Use a plain-text or file-based handoff when the destination is likely to alter characters. Attaching a .json file, or pasting into a code block where the destination supports one, avoids most quote and space substitution. For people who only need the content and not the data, a plain-language description is more readable than raw JSON.

Privacy when using online validators

A web-based validator receives whatever you paste into it. Whether it processes the text only in your browser or sends it to a server, and whether anything is stored, cannot be judged from the page's appearance or wording. Read the tool's published privacy terms, or use a local option such as an editor's built-in validation or a command-line parser, before checking JSON that contains credentials, tokens or personal data.

Bottom line

Validate freely, since it changes nothing. Lint to catch duplicate keys and other legal but risky patterns. Pretty-print for people and minify for machines, but confirm that your formatter preserves large integers, duplicate keys and any key order your software depends on. Keep the original bytes when the text is signed, hashed, compared exactly or written in a commented JSON variant. When sharing JSON, send it as a file or code block to protect it from character substitution.