Python JSON Configuration Testing

Python JSON Configs: Reject Duplicate Keys Before Their Values Disappear

The Prompt Shelf ·

A configuration file can parse successfully while losing a value you meant to keep. If you maintain a Python tool that reads JSON settings, check repeated names before converting the input into ordinary dictionaries.

This lab uses synthetic strings and Python’s standard library. It does not change your configuration, contact a service, or test Claude Code’s own configuration loader.

Synthetic Python JSON example: default parsing keeps retry 9; duplicate-aware parsing rejects two retry names.

A successful parse can hide the problem

In our Python 3.14.3 run, {"retry": 2, "retry": 9} became {'retry': 9} with no exception. That makes a check for the presence of retry insufficient: the setting exists, but the earlier value has disappeared.

Python documents this default and provides object_pairs_hook to inspect each object’s ordered pairs before building the replacement object. See the Python JSON decoder documentation.

RFC 8259, section 4 recommends unique names within an object and describes differing receiver behavior. Do not assume every loader chooses the last value, or describe repeated names as a syntax error that every parser must reject.

Run a small duplicate-aware loader

Save this complete example as check.py, then run python3 check.py using an existing Python installation. All input is synthetic and in memory.

import json

class DuplicateName(ValueError):
    pass

def unique_object(pairs):
    result = {}
    for name, value in pairs:
        if name in result:
            # Do not include configuration names or values in shared logs.
            raise DuplicateName("duplicate name in a JSON object")
        result[name] = value
    return result

def load_unique(text):
    return json.loads(text, object_pairs_hook=unique_object)

if __name__ == "__main__":
    sample = '{"retry": 2, "retry": 9}'
    print("Default loader:", json.loads(sample))
    try:
        load_unique(sample)
    except DuplicateName:
        print("Duplicate-aware loader: rejected")

Expected output from our run:

Default loader: {'retry': 9}
Duplicate-aware loader: rejected

The callback checks each name before inserting it. It rejects a repeated name even when both values are identical. This is a deliberate configuration policy: repeated definitions should be resolved by the author, not silently tolerated until a later edit makes them disagree.

The generic error message avoids printing configuration names and values into shared logs. This small implementation does not identify a line, column, or full object path; do not claim it provides a complete diagnostic report.

What the tests actually covered

We ran 13 synthetic cases on Python 3.14.3, macOS arm64. These results describe the isolated test run; the copy/paste example above demonstrates the core check.

Input situationObserved result
Same name twice, different or identical valuesRejected
Duplicate inside a nested object or an object in an arrayRejected
retry and the escaped spelling \u0072etry in one objectRejected after decoding
retry in two separate objectsAccepted
retry and Retry in one objectAccepted; names differ
A repeated word inside a string valueAccepted
Trailing commaJSONDecodeError
Top-level array or nullAccepted
NaN as a valueAccepted; outside this checker’s policy
Ordinary boolean settingAccepted

This is exact decoded-name comparison. It does not normalize Unicode or detect visually similar characters. The checks are local Python behavior, not a claim about Windows, an IDE, a deployed agent, or a CI provider.

Inspect the original text, not a reformatted dictionary

Our second experiment parsed the duplicate example with ordinary json.loads, serialized that dictionary, and passed the result to load_unique. It was accepted: only one retry remained. Reformatting after a normal parse had already removed the evidence.

For a tool you control, place the duplicate-aware parse at the point where original JSON text enters your program. Perform application-specific validation on the returned value afterward. If your application requires a top-level object, required fields, or a particular type for retry, validate those separately.

When inspecting an existing configuration, keep its original copy. If a repeated name is found, compare the definitions and decide which value the application should receive. Edit a working copy deliberately, then rerun the check. Do not automatically keep the first value, keep the last, or merge values: none of those policies knows the author’s intent.

This procedure is different from configuration precedence across separate files. A user-level and project-level setting may intentionally override each other; this checker only addresses repeated names inside the same JSON object.

Decide whether this check belongs in your loader

Your situationPractical next step
You own the Python loader and want unique namesParse original text with the callback, then validate application rules
You only have an already-parsed dictionaryRecover the original input; the dictionary cannot establish whether duplicates existed
A third-party product owns parsingUse the lab to inspect a copy; verify that product’s rules separately
You need full JSON compliance or a schema checkAdd those policies separately; this helper is not sufficient

The NaN case is intentionally retained as a passing limitation test. Duplicate detection alone must not become a promise of safe or valid configuration. Bound untrusted input size and nesting according to your application’s needs, and do not upload real configuration secrets to online validators.

No paid tool is required for this demonstration. Its purpose is to help you reject ambiguous input before a value silently disappears.

Related Articles

Explore the collection

Browse all AI coding rules — CLAUDE.md, .cursorrules, AGENTS.md, and more.

Browse Rules