美化 JSON

格式化或压缩 JSON 数据,支持配置缩进空格数及对键名进行排序。接收 JSON 文本或 JSON5 输入,输出规范排版的 JSON 字符串。本工具要求输入必须符合合法 JSON/JSON5 语法,无法自动补全或修复被截断的不完整代码。

排查接口配置时,如何整理 JSON

适用于把接口响应或配置片段整理成便于检查和比较的格式。本站提供严格 JSON / JSON5 切换、递归对象键排序以及无缩进、空格或 Tab 输出;这些选项改变排版或解析方式,不是业务数据校验。

操作步骤

  1. 先确认输入格式。接口返回的标准 JSON 选择严格模式;只有明确包含注释、单引号或末尾逗号的配置才选择 JSON5。
  2. 需要阅读层级时使用 2 个空格;需要减少输出体积时选择无缩进;需要比较键顺序时再开启排序。
  3. 检查转换后的值,尤其是大整数。复制或下载结果后,再在目标程序中验证。格式化成功不代表符合目标系统的字段要求。

将有注释的任务配置转为排序后的标准 JSON

输入

{ // local task configuration
 tags: ['backup', 'nightly'], retries: 3, enabled: true,
}

选项

JSON5;对象键排序开启;2 个空格缩进

输出 / 解析结果

{
  "enabled": true,
  "retries": 3,
  "tags": [
    "backup",
    "nightly"
  ]
}

限制与常见问题

  • 严格模式会拒绝上述示例中的注释、未加引号的键、单引号和末尾逗号;不要把这类错误误认为缩进问题。
  • JSON5 输出仍是标准 JSON,注释和原始排版不会保留;NaN 和 Infinity 会被拒绝,而不是悄悄转成 null。
  • 排序会递归处理对象,保留数组元素顺序。数值使用 JavaScript Number,超过安全整数范围的标识符建议在源数据中用字符串表达。

相关任务与参考链接

Organize JSON while checking API configuration

Use this tool to make API responses and configuration fragments easier to inspect and compare. It offers strict JSON/JSON5 selection, recursive object-key sorting and compact, space or Tab output. These options affect parsing and presentation; they do not validate business rules.

Steps

  1. Establish the input format. Use strict mode for standard API JSON, and JSON5 only for configurations intentionally containing comments, single quotes or trailing commas.
  2. Use two spaces to inspect nesting, no indentation for compact output, and key sorting when key order makes comparisons difficult.
  3. Check the resulting values, particularly large integers. After copying or downloading the output, validate it in the target program. Successful formatting does not establish schema compliance.

Convert a commented job configuration into sorted standard JSON

Input

{ // local task configuration
 tags: ['backup', 'nightly'], retries: 3, enabled: true,
}

Options

JSON5; sort object keys; two-space indentation

Output / inspection

{
  "enabled": true,
  "retries": 3,
  "tags": [
    "backup",
    "nightly"
  ]
}

Limits and common issues

  • Strict mode rejects the comments, unquoted keys, single quotes and trailing comma in this example. Changing indentation will not fix a syntax error.
  • JSON5 input still produces standard JSON. Comments and original formatting are lost; NaN and Infinity are rejected rather than silently converted to null.
  • Sorting recurses into objects while preserving array order. Numbers use JavaScript Number; identifiers outside its safe integer range should be represented as strings in the source data.

Related tasks & reference links