STRUCTURED TEXT

Keeping a multiline note in one CSV record

A line break can end a CSV record or sit inside a quoted value. This worksheet compares one record holding a two-line note with two records on separate lines.

Physical lines versus records

A text file is divided into physical lines by line breaks, but a CSV file is divided into records. Most of the time the two match, with one record per line. The CSV specification allows an exception: a field wrapped in double quotes may contain line breaks, and those breaks belong to the field. A file with a three-line note in one cell therefore has more lines than records. Tools that count lines to count rows will be wrong, while a real CSV parser follows the quoting. The shell command that counts lines in a file, for instance, reports every embedded break as an extra row.

One record with a two-line first field

The first input opens a quote, writes line one, breaks the line, writes line two, closes the quote and adds a comma and ok on that second physical line. The parser reads the opening quote and treats everything up to the matching closing quote as a single value. The result is one record with two fields: the first is line one and line two joined by a newline, and the second is ok. Two physical lines, one record.

Two records on two lines

The second input is first,ok on one line and second,ok on the next, with header mode off. No quotes are present, so the line break ends the first record, and the second line starts another. The output is an array holding two inner arrays, each with two fields. The visible layout is similar to the first case: two lines of text, each ending in ok. The difference is only whether the line break sits inside quotes. This pair is a reliable way to test a parser or a file you are unsure about. If a tool reports three rows for the first input, it split on the line break and ignored the quotes, and its counts for your real files will be wrong wherever a note spans lines.

Reading the escaped newline in JSON

The JSON output cannot show a raw line break inside a string, so it writes the newline as a backslash followed by the letter n. In the first case the value displays as "line one\nline two". Those two characters in the JSON represent one newline character in the actual data. JSON defines a short list of such escapes, including backslash n for a line feed, backslash t for a tab and backslash quote for a double quote. They do not mean the source CSV held a backslash and an n. If you search the output for a real line break you will not find one inside the string, and a program that reads the JSON will restore the single newline when it decodes the escape.

Windows and Unix line endings

Line breaks come in more than one style. Windows traditionally uses a carriage return followed by a line feed, and Unix and macOS use a line feed alone. RFC 4180 specifies the CRLF pair as the record separator, and the converter accepts it as well as a lone line feed. For example, a,b then CRLF then c,d with header mode off produces two records, each with two fields. Inside quoted fields, the converter keeps whatever break characters are present, so a note typed on Windows may carry a CRLF pair in its value. A text comparison between that value and the same note typed on a Mac can then report a difference that no one can see on screen.

Where multiline fields come from

Free-text columns are the usual source: comments, descriptions, addresses written on several lines, and support-ticket messages. Spreadsheet applications let a user insert a break inside a cell, usually with a keyboard shortcut, and an export wraps that cell in quotes so the file stays valid. Web forms with a multiline text box feed the same kind of data into exports. The problem shows up when someone edits the export in a plain text editor or with a script that processes it line by line. A tool that splits on line breaks without understanding quotes will cut the record in half and produce rows with the wrong number of fields.

Row width problems from a missing quote

If the closing quote of a multiline field is missing, the parser keeps reading until it finds one, which can swallow many later rows into a single field or reach the end of the file with an error. Conversely, a stray quote opened in the middle of a text field has the same effect. When the converter reports an unexpected record width, count the opening and closing quotes in the area where the structure first goes wrong. Fixing one quote often restores the whole file. Opening the file in a plain text editor with a visible-whitespace setting makes line endings and stray quote characters easier to see than a spreadsheet view does.

What these controls establish

Both runs check the parser's handling of line breaks and do not judge whether a note ought to contain a break. They use two fields per record and a few words per field, so a real file with long notes behaves the same way, only with more text between the quotes. If a downstream system cannot accept newlines inside values, replace them there with a space or another agreed marker, and record that change. A parsed result also does not confirm that the number of columns matches a business schema.

Handling multiline values downstream

Once a value holds a newline, every later stage has to cope with it. A log viewer may show the note as two rows, a label printer may print the break, and a database field may keep it intact. Decide whether the break carries meaning, as in a postal address where each line is a separate part, or whether it was only a wrapped sentence. For addresses, splitting the field into separate columns is cleaner than relying on an embedded break. For prose, replacing the break with a space is often enough. Fixed-format label or form systems may need the opposite, a limit on the number of lines per field, which is a requirement to check in that system's documentation. Whatever you choose, apply it to the parsed values and keep the source file unchanged for reference.

Reproduce the checks

One multiline record

Open the Csv To Json Converter and enter:

CSV input
"line one
line two",ok
First row contains headers
false

Expected output:

Rows

[
  [
    "line one\nline two",
    "ok"
  ]
]

The newline is retained within one value.

Two separate records

Open the Csv To Json Converter and enter:

CSV input
first,ok
second,ok
First row contains headers
false

Expected output:

Rows

[
  [
    "first",
    "ok"
  ],
  [
    "second",
    "ok"
  ]
]

These line breaks delimit records.

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