Reject a JSON array with a duplicate logical identifier
To reject a JSON array with a duplicate logical identifier, decode the JSON first and then apply a collection-level uniqueness rule. json.loads() maps the valid JSON array below to three ordinary dictionaries. Each dictionary is individually valid for this small contract, but the collection is ambiguous because A occurs again at index 2.
validate_unique_ids records the first index for every identifier. When it sees a known identifier again, it produces a precise application error instead of attributing the problem to the JSON decoder. The function also establishes two narrow prerequisites: the root must be an array, and each item must provide a string id. The assertion verifies the exact error promised by this fixture.
This differs from repeated member names inside one JSON object. Python’s JSON documentation notes that repeated object names are accepted by default and the last value is retained. This example detects a duplicate across separate array elements, where the decoder has no knowledge of the application’s identity rule. Python 3.8+ is sufficient for the example, although json.loads is available in earlier releases.
AI assistance disclosure: this article was drafted with AI using a synthetic, locally executable fixture.
Source: Python json documentation.
import json
text = '[{"id": "A"}, {"id": "B"}, {"id": "A"}]'
decoded = json.loads(text)
def validate_unique_ids(items):
if not isinstance(items, list):
raise ValueError("root must be an array")
first_index = {}
for index, item in enumerate(items):
if not isinstance(item, dict) or not isinstance(item.get("id"), str):
raise ValueError(f"item at index {index} needs a string id")
identifier = item["id"]
if identifier in first_index:
raise ValueError(
f"duplicate logical ID {identifier} at index {index}"
)
first_index[identifier] = index
return items
print(text)
print(decoded)
try:
validate_unique_ids(decoded)
except ValueError as error:
assert str(error) == "duplicate logical ID A at index 2"
print(f"validator reports {error}")
[{"id": "A"}, {"id": "B"}, {"id": "A"}]
[{'id': 'A'}, {'id': 'B'}, {'id': 'A'}]
validator reports duplicate logical ID A at index 2