How can an annotation importer detect duplicate JSON object keys?

The object_pairs_hook in loads receives ordered name–value pairs before ordinary dictionary conversion discards duplicate names.

Python example and interpretation

A label key holding cat and then dog reaches the hook twice. An importer can reject that ambiguity according to its data contract.

import json
json.loads('{"label":"cat","label":"dog"}',object_pairs_hook=list)

Result [('label', 'cat'), ('label', 'dog')]

中文

怎样发现 JSON 标注对象中重复出现的键?

loads 的 object_pairs_hook 可接收原始有序键值对,在转换成普通字典前检查重名。

Python 算例与适用条件

同一个标签键先写 cat 再写 dog,两对都会送入钩子。实际导入时可根据数据契约拒绝这种有歧义的记录。

import json
json.loads('{"label":"cat","label":"dog"}',object_pairs_hook=list)

结果 [('label', 'cat'), ('label', 'dog')]

References / 参考来源

Keep reading / 继续阅读