condense-json 1.0:用替换表压缩重复 JSON 字符串
Simon Willison2026-08-04 05:04
condense-json 1.0 发布
condense-json 发布了 1.0 版本。这个小型库已有约一年半时间,本次在应用了一些合理且非破坏性的修复后,正式进入 1.0。
它的用途是:扫描 JSON 中与替换表匹配的字符串或子串,并在输出中用特殊结构表示这些重复内容,从而减少包含重复数据的 JSON 存储体积。
示例
假设输入 JSON 如下:
{
"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"
]
}
]
}
}
}
}
其中,匹配到的子串 with foxes in it 被替换为引用结构 { "$": "1" },并通过 { "$r": [...] } 表示由普通字符串片段和引用片段组成的新字符串。
可逆还原
压缩后的 JSON 可以通过以下函数还原:
uncondense_json(condensed, replacements)
也就是说,只要保留同一份替换对象,就可以在压缩表示和原始 JSON 之间来回转换。
适用场景
这个库的设计目标是让包含重复数据的 JSON 更容易存储,尤其适合那些与其他相关结构共享大量重复字符串的场景。
作者提到,自己将它用于减少 LLM 生成的 SQLite 日志占用空间。