Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What’s a straightforward way to compare records when one file is CSV and the other is JSON?

Geez

First decide which fields matter and how records correspond—for example, matching by a product code and comparing its name and quantity. Then load each format and make a small normalization step for fields whose values may have different Python types. csv.DictReader reads CSV rows as dictionaries, while JSON objects decode as dictionaries and arrays as lists. [S1] [S2]

Here’s a self-contained example. The two text strings stand in for file contents; the records are illustrative. It compares only name and quantity, using sku as the record key.

import csv
import io
import json

csv_text = "sku,name,quantity\nA1,Notebook,3\nB2,Pens,4\n"
json_text = '[{"sku":"A1","name":"Notebook","quantity":3}, {"sku":"B2","name":"Pens","quantity":5}]'

csv_rows = list(csv.DictReader(io.StringIO(csv_text)))
json_rows = json.load(io.StringIO(json_text))

# Explicitly convert this known CSV column; leave other fields unchanged.
for row in csv_rows:
    row["quantity"] = int(row["quantity"])

left = {row["sku"]: row for row in csv_rows}
right = {row["sku"]: row for row in json_rows}
fields = ("name", "quantity")

for sku in sorted(left.keys() | right.keys()):
    if sku not in left or sku not in right:
        print(sku, "missing from", "CSV" if sku not in left else "JSON")
        continue
    for field in fields:
        a, b = left[sku][field], right[sku][field]
        print(sku, field, "match" if a == b else f"DIFF: {a!r} != {b!r}")

The example reports A1 as matching for both fields, and B2 quantity DIFF: 4 != 5. A matching result means only that the selected values compare equal after the conversion shown; it does not establish that every field or record in the original files is equivalent.

Follow-up question

Why convert quantity, and should I convert every CSV value before comparing?

Geez

No—convert only fields where you have a clear rule. In the example, the CSV quantity is read as text ("3") by the default reader, while the JSON number 3 is decoded as an integer, so comparing them directly would report a difference. The CSV documentation describes DictReader and notes that the default reader does not automatically convert fields to numbers; JSON number tokens decode as int or float according to their form. [S1] [S2]

The int() conversion is appropriate only because the example assumes quantities are whole numbers written in a known column. If your values can be blank, decimal, or formatted in another way, define and validate a suitable conversion rather than applying int() blindly. Keep identifiers such as sku as strings if leading zeroes or letter codes matter. Neither format’s documentation determines what your data means, so domain-specific equivalence rules are your decision.

Follow-up question

How do I adapt the example to actual files and handle records that appear in only one file?

Geez

Replace the in-memory strings with file objects. For CSV, open the file with an explicit encoding and newline='' before passing it to csv.DictReader; the CSV documentation recommends this opening pattern. [S1] For JSON, pass an opened text file to json.load(), which deserializes a document from a file object. [S2]

The example already checks the union of keys and prints a missing-record message when a sku occurs on just one side. That makes a missing row visible instead of silently comparing only the overlap. It assumes each file has at most one record per sku; if keys repeat, the dictionary construction overwrites earlier rows. Decide how duplicates should be handled before using this approach on data where repeated keys are possible.

Follow-up question

What should I check before trusting the comparison output?

Geez

Check the assumptions that shape the result: the match key is present and unique, the selected fields exist in both inputs, and each explicit conversion matches the data you expect. Also decide whether blanks, capitalization, whitespace, or missing fields should count as different; the example intentionally does not normalize those. A field-level report is useful for reviewing a small comparison, but this example does not establish that unselected fields match or that either file is complete. For large or irregular datasets, add deliberate validation and error handling rather than treating this short illustration as a full data-quality check.

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.

  1. [S1] Python 3.14 csv — CSV file reading and writing
    docs.python.org · Checked
  2. [S2] Python 3.15 json — JSON encoder and decoder
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work