STRUCTURED TEXT

Finding duplicate CSV headers before object conversion

Header mode turns each CSV row into an object, and an object cannot hold two properties with the same name. This worksheet shows the rejection and the array view that lets you inspect the file anyway.

Why object conversion needs distinct names

With header mode on, the first CSV row supplies property names, and each later row becomes an object whose properties are those names paired with the row's values. If two headers read name, the object would need two properties called name, and a JSON object cannot carry both meanings. Parsers that tolerate duplicates usually keep only the last value and silently drop the earlier one, which loses data. In the worksheet example that would leave name equal to blue and discard red with no sign that anything was removed. The converter takes the safer route and rejects the file, with a message that the header names must be nonempty and unique, so nothing is lost without notice. The rule follows RFC 8259, which advises that names within a JSON object should be unique because receivers behave unpredictably otherwise.

The rejected example

The input is name,name on the first line and red,blue on the second, with header mode on. The two headers are identical, so the mapping from columns to properties is ambiguous: should red or blue be the value of name? The page returns an error mentioning that the names must be nonempty and unique, and it produces no output. The rejection is the intended behavior for this input, and it is the first case on the worksheet. The same message appears for an empty header cell, as in name, followed by a trailing comma, because an empty string cannot serve as a usable property name here.

Turning header mode off to inspect the file

Run the same text with header mode off. The converter then treats every row as data and returns an array of arrays: the first inner array holds name and name, the second holds red and blue. All four cells are preserved. This view is the practical way to look at a file whose header row you do not yet trust. Compare it with the source in a spreadsheet to confirm which column is which, and use it to measure the row widths. Row widths matter because a file with a duplicate header may also have rows of uneven width, and header mode would report that second problem only after the first is fixed. After you correct the header text, you can turn header mode back on.

Where duplicate headers come from

Duplicates often appear when two systems export into one sheet, such as a list that has a name column for the customer and another for the contact. They also come from copied header rows that were pasted twice, or from reports that repeat a label like date for start and end. Some reporting tools add suffixes automatically, producing name and name_1, while others leave the clash. Joins in a database query are another source: selecting the id column from two tables returns two columns both labeled id unless the query gives them aliases. A header such as name,name tells you two columns carry the same label, but it does not tell you which of them is the customer name and which is something else.

Renaming with knowledge of the columns

The fix is to give every column a distinct, meaningful name, but the choice must come from knowing what each column holds. Look at the values under each header, or at the documentation of the system that produced the file. Renaming the second column to contact_name only makes sense if that is what it contains. Deleting a duplicate column to get the conversion to pass can destroy information, and guessing which duplicate to keep is a data-quality decision that belongs with the people who own the data. If the two columns hold identical values in every row, a person can confirm that and then drop one deliberately. If they differ, both are likely needed, and each needs its own clear name.

A valid header row for comparison

With the header changed to name,shade and the data row red,blue, header mode on produces one object where name is red and shade is blue. The converter output lists each property with its string value and keeps the property order from the header. This pair of runs, one rejected and one accepted, makes the rule easy to remember: the number of columns stays the same, and only the uniqueness of the names changes the result. Use this quick pair test when you are debugging a larger file.

Inspecting a wide file for duplicates

In a header row with dozens of columns, duplicates are easy to miss by eye. Convert the file with header mode off and read only the first inner array, which is the header row, then sort its entries or paste them into a spreadsheet and apply a duplicate highlight. If the file is too large to inspect that way, a smaller test file containing only the header and one data row is enough, since the rejection depends on the header alone. Keep the original file untouched and make the corrections in a copy, so that you can compare the old and new headers column by column afterward.

Case, whitespace and near duplicates

JSON property names are case sensitive, so Name and name are different properties, and the converter would accept them. Whitespace also counts as part of the name, so "name" and "name " with a trailing space are distinct. Such near duplicates can pass this check and still confuse people reading the output or downstream code that normalizes names. Review the header text for consistent capitalization and for stray spaces. A header row that was typed by several people is a likely place for these differences. Choosing one naming style, such as lowercase words joined by underscores, and applying it to every column removes most of the confusion before it starts.

Scope of the rule

These examples demonstrate how this converter maps headers to properties. They do not claim that every CSV file must have a header row, since many files hold only data, and header mode off handles those. Even in header mode, the first row is only interpreted as names when you ask for that, so a data file that happens to repeat a value in its first row is safe with the setting off. The check also looks only at the header text. It does not compare the header against any schema or verify that each column holds the type of value its name suggests.

Reproduce the checks

Ambiguous object mapping

Open the Csv To Json Converter and enter:

CSV input
name,name
red,blue
First row contains headers
true

Expected rejection:

nonempty and unique

Both columns would use the same object property.

Inspect without header mapping

Open the Csv To Json Converter and enter:

CSV input
name,name
red,blue
First row contains headers
false

Expected output:

Rows

[
  [
    "name",
    "name"
  ],
  [
    "red",
    "blue"
  ]
]

All cells are preserved as arrays.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks