The 2scraper Amazon Scraper repository documents JSON output. At an import boundary, I’d test what happens if a malformed file contains two price fields inside the same object. The default Python decoder accepts both and keeps the last. That means a reserialized copy can look perfectly normal while hiding the original disagreement.
Written with AI assistance. The Python example uses synthetic data and was checked locally.
Preserve the evidence until the input passes validation
The comparison is within one JSON object. Having the same key in two separate product objects is legitimate. A duplicate in one object is ambiguous, even if both values happen to be identical. I’d reject it before applying a business validator and keep the source run ID so the report owner can request a clean export.
Python’s decoder can pass the ordered pairs to an object hook before creating a dictionary. Use that point to make the decision recursively. A duplicate in a nested price object is just as important as one on the outer record. The test then reads a valid price as a Decimal so the parser policy and the monetary representation are both visible.
from decimal import Decimal import json def unique_pairs(pairs): record = {} for key, value in pairs: if key in record: raise ValueError(f"duplicate field: {key}") record[key] = value return record def load_record(text): return json.loads(text, object_pairs_hook=unique_pairs, parse_float=Decimal) ok = '{"offer":{"price":19.90,"currency":"USD"}}' bad = '{"offer":{"price":19.90,"price":29.90,"currency":"USD"}}' assert load_record(ok)["offer"]["price"] == Decimal("19.90") assert json.loads(bad)["offer"]["price"] == 29.90 try: load_record(bad) except ValueError as error: print(error) else: raise AssertionError("duplicate field accepted") The fixture prints duplicate field: price. If the input format is JSON Lines, I’d attach the line number at the file-reading layer and apply the hook to each record. For an ordinary JSON array, I’d document how the importer reports the failing block without logging the full payload.
I’d keep three fixtures in CI. A nested duplicate should be rejected, two products with their own price fields should pass, and a correct JSON document with a genuinely absent price should reach the business validator. Each catches a different type of regression.
