{"slug": "find-duplicate-json-keys-before-they-overwrite-an-amazon-price", "title": "Find Duplicate JSON Keys Before They Overwrite an Amazon Price", "summary": "A developer demonstrated that Python's default JSON decoder silently accepts duplicate keys within a single object and keeps the last value, so a malformed Amazon scraper export with two price fields can reserialize as a normal-looking record while hiding the original conflict. The proposed fix uses json.loads with an object_pairs_hook that raises on any duplicate key recursively, combined with parse_float=Decimal for monetary values, and three CI fixtures covering nested duplicates, legitimate per-product price fields, and a genuinely absent price.", "body_md": "The [2scraper Amazon Scraper](https://github.com/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.\n\n*Written with AI assistance. The Python example uses synthetic data and was checked locally.*\n\nThe 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.\n\nPython’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.\n\n``` python\nfrom decimal import Decimal\nimport json\n\ndef unique_pairs(pairs):\n    record = {}\n    for key, value in pairs:\n        if key in record:\n            raise ValueError(f\"duplicate field: {key}\")\n        record[key] = value\n    return record\n\ndef load_record(text):\n    return json.loads(text, object_pairs_hook=unique_pairs, parse_float=Decimal)\n\nok = '{\"offer\":{\"price\":19.90,\"currency\":\"USD\"}}'\nbad = '{\"offer\":{\"price\":19.90,\"price\":29.90,\"currency\":\"USD\"}}'\nassert load_record(ok)[\"offer\"][\"price\"] == Decimal(\"19.90\")\nassert json.loads(bad)[\"offer\"][\"price\"] == 29.90\ntry:\n    load_record(bad)\nexcept ValueError as error:\n    print(error)\nelse:\n    raise AssertionError(\"duplicate field accepted\")\n```\n\nThe 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.\n\nI’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.", "url": "https://wpnews.pro/news/find-duplicate-json-keys-before-they-overwrite-an-amazon-price", "canonical_source": "https://dev.to/gresiuseo/find-duplicate-json-keys-before-they-overwrite-an-amazon-price-2bfm", "published_at": "2026-11-23 09:37:26+00:00", "updated_at": "2026-11-23 09:42:18.910842+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["2scraper", "Amazon", "Python"], "also_reported_by": [], "takeaway": "Passing object_pairs_hook to json.loads rejects duplicate keys at any nesting depth before a dict is built, so a malformed record with two price fields raises instead of silently keeping the last value.", "recommendation": {"advice": "Use an object_pairs_hook that raises on duplicate keys when importing scraped JSON, and keep CI fixtures for nested duplicates, valid per-object repeats, and absent fields.", "playbook": "output-verification", "playbook_url": "https://wpnews.pro/playbooks/output-verification"}, "alternates": {"html": "https://wpnews.pro/news/find-duplicate-json-keys-before-they-overwrite-an-amazon-price", "markdown": "https://wpnews.pro/news/find-duplicate-json-keys-before-they-overwrite-an-amazon-price.md", "text": "https://wpnews.pro/news/find-duplicate-json-keys-before-they-overwrite-an-amazon-price.txt", "jsonld": "https://wpnews.pro/news/find-duplicate-json-keys-before-they-overwrite-an-amazon-price.jsonld"}}