[docs]defloads_no_dup(json_str:str)->Any:"""Load json string, and raise KeyError if there are duplicated keys :param json_str: json string :raises KeyError: if there are duplicated keys :return: the parsed object """returnjson.loads(json_str,object_pairs_hook=check_for_duplicate_keys)
[docs]defcheck_for_duplicate_keys(ordered_pairs:List[Tuple[Hashable,Any]])->Dict[Any,Any]:"""Raise ValueError if a duplicate key exists in provided ordered list of pairs, otherwise return a dict. Example: >>> json.loads('{"x": 1, "x": 2}', object_pairs_hook=check_for_duplicate_keys) :raises KeyError: if there is duplicated key """dict_out:Dict[Any,Any]={}forkey,valinordered_pairs:ifkeyindict_out:raiseKeyError(f"Duplicate key: {key}")else:dict_out[key]=valreturndict_out