How can I check for duplicate keys while parsing a JSON object?
Quick answer
Python’s standard JSON decoder accepts repeated object names by default and keeps only the last value, so loading directly into a dictionary can hide duplicates. Pass an object_pairs_hook that checks the ordered key/value pairs and raises an error when a key repeats. If no duplicate is found, the hook can return a dictionary as usual. The hook also checks nested objects. [S1]
The question, the details, the next step.
A practical checklist
Check each key before adding it to a new dictionary; raise a distinct error on repeats.
Pass the hook as
object_pairs_hookto the JSON loading call.Handle duplicate-key errors separately from malformed-JSON errors if needed.
Use the parsed value only after the load succeeds.
Check both top-level and nested duplicates, plus duplicate-free input.
A few more useful answers
Why can’t I check for duplicates after `json.loads()` returns a dictionary?
The standard decoder accepts repeated object names by default and keeps only the last name/value pair, so the earlier occurrence may no longer be visible in the dictionary. Check the pairs during decoding with object_pairs_hook. [S1]
Does the hook automatically tell me the path to a duplicate key?
No. The example detects the repeated key and raises an error, but does not track the object’s path or the key’s position in the input.
What does this approach do to the JSON input?
It validates while decoding; it does not edit or rewrite the input. The hook returns dictionaries for objects without duplicate keys, or raises when it finds one.
Sources
References used for this guide.
- [S1] Python json module: object_pairs_hook and repeated names docs.python.org · Checked
