PlayTools

JSON 格式化:提升可读性,减少联调失误

格式化、验证和压缩 JSON,带语法高亮和错误检测,全程在浏览器本地运行。

Runs in your browser — nothing you enter is uploaded

JSON 格式化:提升可读性,减少联调失误

JSON 是现代 API 的数据交换格式。缩进排版能快速暴露漏逗号、括号不配、尾部杂质等问题;压缩可减小体积,便于在生产环境传输。

适用场景:从日志粘贴响应样例、准备测试夹具、教学演示对象与数组嵌套。校验与格式化在浏览器本地完成,输入默认不上传服务器。

隐私:大型 JSON 可能含令牌或个人数据,截屏或外发前先脱敏。可与 Base64、URL 工具串联排查编码层问题。

边界:重复键名在规范中不推荐,部分解析器采用“后者覆盖”;JSON 标准不含注释,若用 JSON5 需在流水线中显式支持。

工作流:开发阶段优先格式化审阅,真正发包再按需压缩;仓库里可读版本更利于评审。

What Format and Minify actually do

Both buttons parse your input with the browser’s built-in JSON parser and write it back out — with 1, 2 or 4 spaces of indentation for Format, or with no whitespace at all for Minify. If the input is not valid JSON, nothing is rewritten and the parser’s error message is shown instead. Because the output is regenerated from the parsed data rather than edited in place, a few things change in ways worth knowing:

  • Duplicate keys: {"k":1,"k":2} becomes {"k":2}. The last value wins silently and the earlier one is dropped — a common source of “my setting is ignored” bugs.
  • Big integers lose precision: 12345678901234567890 becomes 12345678901234567000, because numbers are stored as 64-bit floating point and only integers up to 9,007,199,254,740,991 are exact. Send large IDs as strings.
  • Number spelling is normalised: 1.0 becomes 1 and 1e3 becomes 1000 — the same values, written differently.
  • Integer-like keys move first: {"b":1,"2":0,"a":2,"1":0} comes out as {"1":0,"2":0,"b":1,"a":2}. Other keys keep their original order.
  • Escapes are decoded: "caf\u00e9" is written back as "café".

Worked example: fixing invalid JSON

This looks like JSON but is rejected:

{
  'name': "Ada",
  "langs": ["en", "fr",],
  "active": True
}

Three problems: property names and strings need double quotes, a comma is not allowed after the last item in an array or object, and the literals true, false and null must be lowercase. The corrected version formats cleanly, and minified it becomes {"name":"Ada","langs":["en","fr"],"active":true} — 48 characters instead of 74 with 2-space indentation.

Minifying saves more as nesting grows: a three-user sample with ids, names, roles and an active flag goes from 412 characters pretty-printed to 203 minified. Over HTTP, gzip or Brotli compression narrows that gap considerably, so readable JSON in config files and fixtures costs very little.

Reading the error message

Error messages come from your browser, so the wording varies, but current Chrome and Edge report a position and a line and column, for example “Expected property name or '}' in JSON at position 4 (line 2 column 3)”. Position counts characters from zero. The real mistake is usually at that point or just before it — often a missing comma or quote on the previous line.

“Unexpected non-whitespace character after JSON” means there is more than one value in the input, such as two objects one after another. That is how NDJSON (newline-delimited JSON) log files are written; format one line at a time.

JSON, JSONC, JSON5 and NDJSON

  • JSON (RFC 8259): what this tool accepts — no comments, no trailing commas, double quotes only.
  • JSONC: JSON with // and /* */ comments, used by tsconfig.json and VS Code settings. Remove the comments before validating here.
  • JSON5: a looser superset allowing single quotes, unquoted keys, trailing commas and hexadecimal numbers. Fine for hand-edited config if your tooling supports it; not interchangeable with JSON APIs.
  • NDJSON / JSON Lines: one complete JSON value per line, common for logs and data exports. Each line is valid JSON on its own; the file as a whole is not.

Choosing an indentation

Two spaces is the convention in JavaScript projects (package.json, most API documentation) and keeps deep nesting readable on narrow screens. Four spaces matches Python’s json.dumps(indent=4) and many Java style guides. One space is a compromise for files that are diffed often but still read by humans. Pick one per repository and let your formatter enforce it, so reviews show real changes rather than whitespace.

In-depth guides

These free tools pair well with this page — open them in a new tab to finish your workflow.

常见问题

"格式化"和"压缩"对JSON有什么作用?

格式化(美化打印)添加空白、缩进和换行,使JSON便于人类阅读。压缩去除所有不必要的空白,生成最紧凑的形式,节省API响应的带宽。

为什么我的JSON验证失败?

常见JSON错误包括:最后一个键/值对后有尾随逗号(JSON不允许)、字符串使用单引号而非双引号、属性名未加引号、或数字有前导零。此验证器会高亮显示错误位置。

JSON和JavaScript对象有什么区别?

JSON是一种数据交换格式,是JavaScript的严格子集。主要区别:JSON属性名必须是双引号字符串;JSON值不能是undefined、函数或Symbol;JSON不支持注释。

JSON可以包含注释吗?

不可以。JSON规范(RFC 8259)不允许注释。如果需要在配置文件中使用注释,可以考虑使用JSON5、HJSON或YAML。

什么是JSON Schema?

JSON Schema是一种允许您验证JSON文档结构和内容的词汇规范。您可以定义规则,如必填属性、允许的类型和值范围。它用于API契约验证和表单输入验证。