工具 指南
developer 免费 无需注册

JSON 格式化与校验

格式化、校验和压缩 JSON — 精确定位语法错误。

正在加载工具…

关于此工具

粘贴任意JSON,获得美化或压缩后的版本。无效JSON会高亮显示错误的确切行号和列号。支持最大10 MB的文件。

使用方法

  1. 1 将JSON文本粘贴到输入区域。
  2. 2 点击"美化"以获得可读的缩进输出,或点击"压缩"以去除空白字符。
  3. 3 如果存在错误,确切的行号和列号将被高亮显示。
  4. 4 点击"复制"将格式化后的JSON复制到剪贴板。

What "formatting" JSON actually does

JSON (JavaScript Object Notation) is a plain-text format for structured data: nested objects in curly braces, arrays in square brackets, and key/value pairs separated by commas. A machine doesn't care whether that text is squeezed onto one line or spread across fifty — both parse to the same data. Formatting changes only the whitespace between tokens. Beautifying inserts newlines and indentation so a human can read the structure; minifying removes every optional space so the payload is as small as possible. This tool does both by parsing your text into a real object with the browser's native JSON.parse(), then re-serialising it with JSON.stringify() — either with two-space indentation or with none.

Because it round-trips through a genuine parser, formatting here is also validation. If the parser can't read your text, it can't reformat it, and you get the exact error message instead. Everything runs locally in your browser; the JSON you paste is never uploaded.

A worked example

Paste this minified line:

{"user":{"name":"Ada","roles":["admin","editor"],"active":true},"count":2}

Beautify turns it into a readable tree:

  • user is an object containing name, roles, and active
  • roles is an array of two strings
  • active is the boolean true (not the string "true")
  • count is the number 2

Minify reverses the process, collapsing it back to the single compact line — useful when you need to embed JSON in a URL, a config value, or an API request body where every byte counts. The tool also reports the input size in bytes, so you can see exactly how much minifying saves.

Reading and fixing validation errors

The most valuable feature is the error message, because malformed JSON is the cause of countless silent API failures. The parser pinpoints where it gave up. The usual culprits:

  • Trailing comma. {"a":1,} is invalid — JSON forbids a comma before a closing brace or bracket, even though JavaScript object literals allow it.
  • Single quotes. Keys and strings must use double quotes. {'a':1} fails; it must be {"a":1}.
  • Unquoted keys. {a:1} is a JavaScript object literal, not JSON. Every key needs quotes.
  • Missing or extra comma between items. The error usually points at the token right after the problem, so check the line just before the reported position.
  • Smart quotes. Pasting from a word processor can replace " with curly “ ”, which the parser rejects.

Why JSON forbids comments

A frequent surprise: // like this or /* this */ will not parse. Comments were deliberately left out of the JSON spec so the format stays a pure, unambiguous data interchange. If your file has comments, it is really JSONC (JSON with Comments, used by tools like VS Code's settings) or JSON5. Strip the comments before formatting here, or use an editor that understands those relaxed dialects. The same applies to trailing commas, which JSON5 permits but standard JSON does not.

Practical use cases

  • Debugging API responses. Paste a one-line response from a server log and beautify it to see the structure at a glance.
  • Cleaning up before committing. Reformat configuration files (package.json, tsconfig.json) so diffs stay consistent across a team.
  • Shrinking payloads. Minify a large config before pasting it into an environment variable or a query string.
  • Sanity-checking generated JSON. Validate output from a script or an AI model before sending it downstream, catching syntax errors early.

Tips and common pitfalls

  • Numbers keep their type. Note that re-serialising can normalise number formatting — 1.0 becomes 1, and very large integers may lose precision because JavaScript numbers are 64-bit floats. If you need exact big-integer fidelity, treat such values as strings.
  • Key order is preserved. The tool keeps your keys in their original order rather than sorting them, so the only change you see is whitespace.
  • Duplicate keys collapse. If an object has the same key twice, the parser keeps only the last value — a silent data loss worth knowing about.
  • Mind the size. Parsing happens in memory, so files past roughly 10 MB can make the browser sluggish. For multi-megabyte data, a command-line tool such as jq is faster.
  • Validating isn't schema-checking. This tool confirms your JSON is syntactically correct, not that it matches the shape an API expects. For that, use JSON Schema validation.

常见问题

{# Alpine.js — self-hosted. (The previous jsdelivr CDN tag had a stale SRI integrity hash, so the browser refused to run it and window.Alpine was never defined — silently breaking every FAQ accordion and Alpine tool.) #}