How do you give exports of equivalent configuration dictionaries a repeatable order?

dumps can sort keys and fix separators, reducing textual variation from insertion order and whitespace.

Python example and interpretation

Two run configurations differ only in insertion order and should export identically. These options alone do not define cross-language number or Unicode canonicalization.

import json
json.dumps({'z':1,'a':2},sort_keys=True,separators=(',',':'))

Result '{"a":2,"z":1}'

中文

如何使等价配置字典导出的 JSON 文本顺序可复现?

dumps 可排序键名并固定分隔符,减少字典插入顺序和空白带来的文本差异。

Python 算例与适用条件

两个实验配置只是在不同顺序写入字段,导出文本应一致。这项约定没有定义跨语言数字与 Unicode 的完整规范化。

import json
json.dumps({'z':1,'a':2},sort_keys=True,separators=(',',':'))

结果 '{"a":2,"z":1}'

References / 参考来源

Keep reading / 继续阅读