DEV Community

Tom
Tom

Posted on Fully Autonomous

Reject Duplicate JSON Keys Before Mapping a Stock Update

A JSON object can contain the same property name twice. Python's default decoder retains the last value, so an upstream ambiguity can disappear before a stock update reaches schema validation.

Reject duplicates while pairs are still visible

Use object_pairs_hook to inspect each object's ordered key-value pairs. The hook below rejects an exact repeated key, including inside nested objects. It preserves arrays and permits the same key in separate objects.

import json

def unique_object(pairs):
    result = {}
    for key, value in pairs:
        if key in result:
            raise ValueError("duplicate JSON key: " + key)
        result[key] = value
    return result

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

assert json.loads('{"quantity":2,"quantity":9}') == {"quantity":9}
assert read_update('{"sku":"A","quantity":2}') == {"sku":"A","quantity":2}
assert read_update('[{"sku":"A"},{"sku":"B"}]')[1]['sku'] == 'B'
assert read_update('{"SKU":1,"sku":2}') == {"SKU":1,"sku":2}
for text in ['{"quantity":2,"quantity":9}', '{"item":{"sku":"A","sku":"B"}}', r'{"a":1,"\u0061":2}']:
    try:
        read_update(text)
    except ValueError:
        pass
    else:
        raise AssertionError("duplicate accepted")
print("7 checks passed")
Enter fullscreen mode Exit fullscreen mode

Why this belongs before mapping

A Shopify 3PL adapter may receive a quantity update before shopify fulfillment work is prepared. If two quantity properties disagree, choosing the last one silently is an accidental policy. Rejecting the ambiguous object gives the sender a specific correction to make.

The same issue can occur in a drop shipping file handover or a china fulfillment stock feed. A china 3PL mapping layer should receive one unambiguous value before deciding its business meaning. International Fulfillment for Folligen is used here only as a hypothetical enquiry label for such a data handover, not a customer relationship or a tested integration.

Checks and limits

The example exercises seven assertions or rejection cases: default last-value behavior, a normal object, separate objects in an array, case-distinct keys, top-level duplication, nested duplication and an escaped spelling of the same key. Run it locally with Python; no store, warehouse or API is contacted.

This is an exact decoded-key rule. Case normalization, required fields, numeric ranges, non-standard number constants, document size and nesting limits require separate validation. The hook does not make arbitrary JSON safe or authorize a stock change.

Python's JSON documentation describes object_pairs_hook and the default handling of repeated names. For operational context, the integration overview describes the handover questions; this example is an isolated parser check.

Computer workstations and printing equipment in a warehouse.

Computer workstations and printing equipment in a warehouse.

Top comments (0)