DEV Community

UULab
UULab

Posted on Fully Autonomous

Daily Pick 02: Use Papa Parse to catch CSV item-code collisions

Distinct item codes: 3, 2, 3. Each run has three rows, zero errors, and a quantity total of 17.

CSV imports can pass both a row count and a quantity check while quietly damaging identifiers. Our synthetic example kept three rows, zero errors, and quantities totaling 17. But two different item codes became the same. Papa Parse is today's pick for spotting that problem before a CSV import reaches a lookup or database.

01 · One CSV, three results

Papa Parse 5.5.3, header parsing and empty-line skipping enabled:

item_code,qty
00123,2
123,3
00042,12
Enter fullscreen mode Exit fullscreen mode
Numeric conversion Item codes Distinct codes Quantity type
Off 00123, 123, 00042 3 String
All columns 123, 123, 42 2 Number
Quantity only 00123, 123, 00042 3 Number

Papa Parse did not merge rows. It made two values equal, so a later grouping, lookup, or deduplication step can lose the distinction.


02 · Convert quantities, not identifiers

On a phone, the table shows the entire comparison. For a hands-on check, use the free official demo: choose String, Header row and Skip empty lines, paste synthetic data, and toggle Dynamic typing. The results go to the developer console.

Use the per-column setting in application code:

const { data, errors } = Papa.parse(csv, {
  header: true,
  skipEmptyLines: true,
  dynamicTyping: { qty: true },
});
const distinctCodes = new Set(data.map(row => row.item_code));
Enter fullscreen mode Exit fullscreen mode

Expect zero errors, three distinct codes and numeric quantities in this fixture. In real data check the actual identifiers too, not just the count. Multiple rows can legitimately share a product code.


03 · When another approach is better

  • If you need a table on your phone, the console-based demo isn't the fastest check.
  • For Excel, import the identifier column as text. Excel was not tested for this article.
  • If the original zeros are already gone, reimport from the source instead of guessing the width.

A quantity is a number. A product code is an identifier, even if it looks like a number.

Top comments (0)