How can I compare CSV and JSON records using Python’s standard library?
Quick answer
Read CSV rows with csv.DictReader and JSON records with json.load(), then compare the same chosen fields using a common representation. CSV reader values are not automatically numeric in its default mode, while JSON numbers decode as Python numbers, so convert a known CSV numeric field explicitly. Match records by a chosen key and report each selected field’s result.
The question, the details, the next step.
A practical checklist
Choose a record key and verify it is present in both inputs.
Select only the fields that need comparison.
Write down and apply explicit type conversions for selected fields.
Decide how blank values, missing fields, and duplicate keys should be treated.
Review both field differences and records missing from either side.
A few more useful answers
Does CSV automatically preserve numbers as numbers?
Not with the default csv reader: its fields are not automatically converted to numeric types. Convert a known numeric column deliberately if that is appropriate for your data. [S1]
Does a matching report prove the files are identical?
No. This example compares only the selected fields for records matched by the chosen key; it does not check other fields or establish that either input is complete.
Sources
References used for this guide.
- [S1] Python 3.14 csv — CSV file reading and writing docs.python.org · Checked
- [S2] Python 3.15 json — JSON encoder and decoder docs.python.org · Checked
