DATA REPRESENTATION CHECKS

Checking whether the first CSV record is data or a header

The header option decides whether the first CSV record names the fields or is data. This guide runs one two-record file both ways.

One file, two readings

A CSV file does not label its own first row. Nothing in the text says whether the first line is a set of column names or an ordinary record. RFC 4180 makes the header optional and says only that a header line, if present, has the same format as the others. The reader has to be told. The converter takes that choice from the header option, so the same characters can become objects with named properties or plain rows of values, depending on one setting. That makes the option the first thing to check whenever converted data looks wrong. A file that arrives from a database export, a report generator or a hand-built spreadsheet may or may not carry a title row, and two files with identical layout can differ in this respect.

The same input with headers on

Take two records: name, then Ada. With the header option on, the first record becomes the property name and the second becomes the data. The output is a single object with the property name equal to Ada. Only one data row exists in the result, because the first row was consumed as the header. This is the form most applications want when the file has a title row, since each record can be read by field name and not by position. With a wider file, the header a,b followed by the row 1,2 gives one object with a equal to 1 and b equal to 2. Adding a column to the file later does not shift the meaning of the existing ones, because code looks fields up by name and not by index.

The same input with headers off

With the option off, both records are data. The output is a list of two rows: the first holds the string name and the second holds the string Ada. Nothing is used as a key, and each row is a list of values by position. This is the faithful reading of a headerless file, where every line is a record. The shape differs from the first case: lists of lists in place of a list of objects, and two rows in place of one. Position now carries the meaning: the first value in each row is the first column, and code that reads by index works on every row alike. This form suits files with no title row, sensor logs, and any case where you plan to supply your own column names afterward.

The word name proves nothing

Neither setting infers the intended role from the content. The word name looks like a header to a human reader, because it is the sort of label a column gets, but the converter does not guess. If the same file had a first row of Ada, the result would still depend on the option. Content heuristics are unreliable, since a data value can look like a label and a label can look like data. The role is a property of the source format and has to come from its definition. A file listing first names, for instance, may start with a record such as Ada that is a genuine person and not a label, and no program can tell the two cases apart from the text alone.

Losing a data row

Treating a headerless file as if it had headers drops its first data record from the data and uses it as names. If that record was a real measurement, such as a first customer or a first reading, the result is missing it, and every property is named after a value. The error is hard to see in a long file, since the output still looks well formed. A count of rows against the expected total catches it, as does a glance at the property names for values that look like data.

Keeping a header as a data row

The opposite mistake keeps the header line as a record. The output then has a row of labels where the data should start, so numeric columns contain text in the first row, and sorting, sums and type conversion go wrong. Charts show a spurious first point, and a total fails or silently skips the label. A first row that is entirely text above columns of numbers is a common sign. Switching the option on moves the labels into property names and gives a data set whose rows are all data. The header option also affects the row count by one, so a file of 50 lines gives 50 rows with it off and 49 objects with it on, a quick way to confirm which mode produced a given output.

Reading the format definition

Choose the option from what the source documents, not from how the file looks. A data dictionary, an export setting or a producer's specification says whether the first line is a header. If none exists, inspect the first two rows. A header usually has text in columns where later rows have numbers or dates. Where the evidence is weak, ask the producer instead of guessing, since a wrong choice affects every record.

Other things the option changes

The header option also changes how many fields the converter expects per row and how it reports a mismatch. With headers on, each data row is matched against the names, so a row with extra or missing fields can raise an issue, and duplicate names are rejected. With headers off, rows are simply lists, and nothing is matched to names. The same input can therefore pass in one mode and fail in the other, which is useful to know when debugging an import.

What the example covers

The sample uses a single column and one data value so that the effect of the option is the only difference. Real files have many columns, quoted fields and longer values, but the principle holds. The converter applies the setting you give it and reports the result. It does not decide the role of the first row for you, and it does not rewrite the source. Keep the original text and the chosen option together in your notes, so the conversion can be repeated and checked. If a later reader gets different output from the same file, a different option value is the first suspect.

Reproduce the checks

Header-enabled objects

Open the Csv To Json Converter and enter:

CSV input
name
Ada
First row contains headers
true

Expected output:

Rows

[
  {
    "name": "Ada"
  }
]

The first record becomes the property name.

All records as arrays

Open the Csv To Json Converter and enter:

CSV input
name
Ada
First row contains headers
false

Expected output:

Rows

[
  [
    "name"
  ],
  [
    "Ada"
  ]
]

Both source records remain data.

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