JSON.CN让代码更轻
JSON 整理与类型生成可运行案例

多语言词条:压缩 JSON 并保留字段和值

移除 多语言词条 响应中的排版空白,保留字符串内部的空格、中文和嵌套层级。压缩 JSON 不是 gzip 压缩,也不是加密。

浏览器本地处理合成数据同场景 32 个案例 ↗

示例工作台

在完整工具中打开 ↗
JSON 整理与类型生成minify
运行结果
{"ok":true,"data":{"messages":[{"messageKey":"demo-translations-1","sourceText":"欢迎使用演示","revision":1,"reviewed":true,"namespace":"common","translations":{"en":"Welcome to the demo","locales":["zh-CN","en"]}},{"messageKey":"demo-translations-2","sourceText":"确认演示订单","revision":2,"reviewed":false,"namespace":"checkout","translations":{"en":"Confirm demo order","locales":["zh-CN","en"]}}],"summary":{"total":2,"note":null}}}

原例已核验,可修改输入后重新运行。

⌘ / Ctrl + Enter 运行
01

场景与操作说明

词条键与展示文本需要分开保存。翻译内容变化并不代表词条标识变化,导出时还应保留语言与命名空间。

revision 为示例修订号;是否单调递增属于实际版本规则。

  1. 核对 多语言词条 的字段层级,保留 messageKey 的字符串类型。
  2. 修改一项输入并运行,观察结果中的值或路径如何变化。
  3. 载入对照样本,复现下面的变化;恢复原例会替换当前输入。
02

对照样本

尾逗号是语法问题,移除空白不能把它变成合法 JSON;对照输入预期报错。

查看对照输入
{
  "ok": true,
  "data": {
    "messages": [
      {
        "messageKey": "demo-translations-1",
        "sourceText": "欢迎使用演示",
        "revision": 1,
        "reviewed": true,
        "namespace": "common",
        "translations": {
          "en": "Welcome to the demo",
          "locales": [
            "zh-CN",
            "en"
          ]
        }
      },
      {
        "messageKey": "demo-translations-2",
        "sourceText": "确认演示订单",
        "revision": 2,
        "reviewed": false,
        "namespace": "checkout",
        "translations": {
          "en": "Confirm demo order",
          "locales": [
            "zh-CN",
            "en"
          ]
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
,}

对照样本的实际结果

Expected double-quoted property name in JSON at position 763 (line 39 column 2)