← 文章 / 编程开发
Simon Willison 2小时前 · 2026-08-03 11:26:51 · 0 阅读

condense-json 1.0:用替换字符串压缩 JSON 的 Python 库

发布 condense-json 1.0 — 使用替换字符串压缩 JSON 的 Python 函数

我正努力在发布 1.0 版本时更大胆一些。这个小库已经有一年半的历史了——我应用了一些合理且无破坏性的修复,并为其发布了重要的 1.0 版本。

以下是从 README 中摘录的一个示例,展示了它的功能:

{
  "foo": {
    "bar": {
      "string": "This is a string with foxes in it",
      "nested": {
        "more": ["Here is a string", "another with foxes in it too"]
      }
    }
  }
}

结合一个替换对象:

{"1": "with foxes in it"}

然后 condense_json(input_json, replacements) 会产生以下结果:

{
  "foo": {
    "bar": {
      "string": {"$r": ["This is a string ", {"$": "1"}]},
      "nested": {
        "more": ["Here is a string", {"$r": ["another ", {"$": "1"}, " too"]}]
      }
    }
  }
}

它会扫描替换对象中存在的字符串或子字符串,并在输出中用特殊的 {"$r": ...} 语法替换它们。

你可以通过 uncondense_json(condensed, replacements) 逆转这一效果。

其目的是让存储包含来自其他相关结构的重复数据的 JSON 变得更加容易。我用它来节省 LLM 生成的 SQLite 日志空间——参见 PR #1586 了解该功能的最新迭代。

原始来源: Simon Willison

评论 (0)