Skip to content
Kilnfile

How to format and validate JSON

By . Published , last updated .

To format and validate JSON, paste it into Kilnfile's JSON Formatter, choose 2 spaces, 4 spaces, Tab or Minified under Indentation and click Format JSON; invalid input is rejected with the line and column of the first error. The checks follow RFC 8259: double-quoted keys, no trailing commas, no comments and no leading zeros.

Step by step

  1. Open JSON Formatter. Go to the JSON Formatter tool. It parses with the browser's own JSON engine, so API responses and config files containing keys or customer data are never uploaded.
  2. Paste the JSON into the Input box. Paste the whole document, including the outer braces or brackets. A fragment such as a bare key and value is not valid JSON on its own.
  3. Choose Indentation. Pick 2 spaces (the convention for JavaScript and most web projects), 4 spaces (common in Python and .NET), Tab, or Minified (no whitespace) for the smallest output.
  4. Tick Sort keys alphabetically if you want a stable order. Sorting makes two exports of the same data easy to compare in Diff Checker. Leave it off if the key order carries meaning for a human reader.
  5. Click Format JSON. Valid input is reformatted instantly. Invalid input shows the error message with the line and column of the first problem so you can go straight to it.
  6. Copy the output. Click Copy output and paste it back into your editor, config file or API client.

What makes JSON invalid?

JSON is defined by RFC 8259 and is stricter than the JavaScript it grew out of. The rules that catch most people: keys must be strings in double quotes, so {name: 1} and {'name': 1} are both invalid. There are no trailing commas, so [1, 2, 3,] fails. There are no comments, so // and /* */ are errors. Numbers cannot have leading zeros (007) or a trailing decimal point (5.), and NaN and Infinity are not valid values. Strings use double quotes only, and a literal newline inside a string must be written as \n.

Most of these are habits carried over from JavaScript, Python dictionaries or hand-edited config files, which is why a file that looks fine to the eye fails the moment an API or parser reads it.

How do I read the error message?

The formatter reports the line and column where parsing first failed. That is where the parser gave up, which is usually at or just after the real mistake. A trailing comma shows up as an unexpected closing bracket on the next line; a missing comma shows up as an unexpected string where the next key begins; an unquoted key is reported at the key's first character. Fix the reported position, click Format JSON again, and repeat until it passes. Large files rarely have more than two or three problems.

Which indentation should I use?

Match the project you are putting the file into. Two spaces is the de facto standard for JavaScript, TypeScript, package.json and most web tooling. Four spaces is common in Python projects and in files written by .NET and Java tools. Tabs are rarer in JSON but some editors default to them. Minified removes every byte of whitespace and is what you want for data sent over a network or embedded in a script tag; a 2 MB pretty-printed API response often minifies to 1.3 MB.

When should I sort the keys?

Sorting keys alphabetically is useful whenever you need to compare two JSON documents, because most systems emit keys in whatever order they were built and a diff fills with noise. Sort both, then paste them into Diff Checker and only real changes remain. It is also helpful for config files edited by several people, since everyone finds settings in the same place. Do not sort when the order is meaningful to a reader, such as a list of steps written as an object, or when a downstream tool is sensitive to key order, which is rare but not unknown in older systems.

What about JSON with comments, JSON5 and YAML?

Files such as tsconfig.json and VS Code settings allow comments and trailing commas, but that dialect is JSONC, not JSON, and a strict validator will reject it. Strip the comments before formatting, or format only the sections you need. JSON5 goes further with unquoted keys and single quotes. YAML is a different format altogether. If you need to move tabular data between formats, CSV to JSON and JSON to CSV handle the common case of a flat list of records.

Frequently asked questions

Is it safe to paste API keys or customer data into the formatter?

The formatting runs in your browser tab using the built-in JSON parser, and nothing is sent to a server. You can confirm this in the browser's Network tab. Even so, redact secrets before sharing a formatted file with anyone else.

Why does my JSON fail with a trailing comma when it works in my code?

JavaScript object literals allow trailing commas; JSON does not. Your code is reading an object, not parsing a JSON string. Remove the final comma before the closing brace or bracket.

Can the formatter fix the errors for me?

No. It reports the line and column of the first problem and leaves the fix to you, because guessing at intent can silently change data. Most fixes take a few seconds once you know where to look.

How large a file can I format?

Several megabytes is fine on a laptop; a 20 MB file will format but the text box becomes slow to scroll. For very large files, minify first or split the data into smaller documents.

Does formatting change the data?

No. Only whitespace and, if you tick the option, key order are changed. Values, nesting and the number of elements are identical, and numbers are preserved as written apart from redundant formatting that the parser normalises, such as 1.0 becoming 1.

Data source: RFC 8259 (The JavaScript Object Notation Data Interchange Format), ECMA-404, and MDN documentation for JSON.parse error reporting.

Cite this page: “How to Format and Validate JSON”, Kilnfile, https://kilnfile.com/guides/how-to-format-and-validate-json/

Tools mentioned in this guide

Related guides