cd /news/developer-tools/find-duplicate-json-keys-before-they… · home › topics › developer-tools › article
[ARTICLE · art-174604] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Find Duplicate JSON Keys Before They Overwrite an Amazon Price

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.

recommendation

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.

more in playbook: Verifying model output
by read2 min views2 publishedNov 23, 2026

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.

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.

── more in #developer-tools 4 stories · sorted by recency
── more on @2scraper 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/find-duplicate-json-…] indexed:0 read:2min 2026-11-23 · —