Reject Duplicate JSON Keys Before Validating Inventory Fields
An inventory message can contain two values for the same field. If a parser quietly keeps the last value, a later schema check sees only one of them. The ambiguity has already disappeared. For global dropshipping, data used in warehousing and fulfillment should cross an explicitly defined validation boundary rather than depend on that silent choice.
Python's standard JSON decoder accepts repeated object names by default and keeps the last value. Its object_pairs_hook option lets the application inspect the original sequence of name-value pairs. Rejecting duplicates there makes the decision visible before validating the resulting inventory object.
Parse first, then validate the record
This example accepts one object containing a non-empty string SKU and a non-negative integer quantity. It rejects additional fields. These are example interface rules, not a claim about a warehouse platform's existing API.
import json
def unique_object(pairs):
result = {}
for key, value in pairs:
if key in result:
raise ValueError(f"duplicate field: {key}")
result[key] = value
return result
def reject_constant(value):
raise ValueError(f"invalid JSON constant: {value}")
def read_stock(raw):
record = json.loads(raw, object_pairs_hook=unique_object,
parse_constant=reject_constant)
if type(record) is not dict or set(record) != {"sku", "quantity"}:
raise ValueError("expected sku and quantity only")
if not isinstance(record["sku"], str) or not record["sku"]:
raise ValueError("invalid sku")
if type(record["quantity"]) is not int or record["quantity"] < 0:
raise ValueError("invalid quantity")
return record
print(read_stock('{"sku":"0007","quantity":3}'))
The output is {'sku': '0007', 'quantity': 3}. A second quantity field raises ValueError instead of overwriting the first. The hook also runs for nested objects, although this particular record schema does not accept nested values for its two fields.
Make the remaining parser choices explicit
The exact integer type check excludes true: Python treats booleans as integers in some other checks. It also excludes 3.0, even though that value may look interchangeable with 3 to a reader. If the sender needs a different numeric contract, change and test the contract deliberately.
parse_constant rejects the non-standard NaN and infinity spellings accepted by Python's decoder. It does not make this a universal strict-JSON validator or a resource-limit mechanism. A receiving service still needs appropriate request-size limits before parsing untrusted input.
The code was executed with Python 3.12.14 using local strings. Validation covered valid records, repeated field names, nested duplicates, malformed input, wrong field sets, invalid SKUs, booleans, fractional and negative quantities, and non-standard constants. No customer messages or live inventory were used.
See the official Python JSON documentation for decoder options. Rejecting an ambiguous message does not reconcile physical stock or authorize an inventory adjustment. It gives the caller a clear failure that can be reported and investigated before the data enters a later workflow.

Computer workstations and printing equipment in a warehouse.
All Rights Reserved