STRUCTURED TEXT

Writing a literal quotation mark inside a CSV field

To put a literal double quote inside a quoted CSV field, write it twice. This guide shows how the parser reads doubled quotes, how an unclosed quote breaks a record, and how to avoid both mistakes when you write or repair CSV text.

Why quotes need a rule at all

A CSV file is plain text in which commas separate fields and line breaks separate records. That works until a value contains a comma, a line break or a quotation mark of its own. A name such as Smith, Jane would be split into two fields, and a note with a line break would be split into two records. The usual remedy, described in RFC 4180, is to wrap such a field in double quotes. The wrapping creates a new problem, because a double quote inside the value now looks like the end of the field. The format answers with a short rule: inside a quoted field, each literal double quote is written as two consecutive double quotes. The parser reads the pair as one character of content and keeps going.

Reading the default example

The worked input is the single line "Say ""hello""",ok with header mode off. Read it from the left. The first character is a quote, which opens a quoted field. The text Say and a space follow. Then comes a pair of quotes, which the parser turns into one literal quote. After the word hello there are three quotes in a row. The first two are another doubled pair, one literal quote, and the third closes the field. A comma follows, so a new field begins, and ok is a plain unquoted value. The Csv To Json Converter returns one row with two values: Say "hello" and ok. The outer quotes and the doubling are encoding. They are not part of the data, which is why the parsed value shows ordinary quotation marks.

Counting quote characters

A simple count helps when you write or audit a field by hand. If the value contains k literal quotation marks, the quoted form contains 2k plus 2 quote characters, since each literal one is doubled and two more wrap the field. The value Say "hello" has two literal quotes, so its encoded form has six, which matches the example above. This also gives a quick validity test. Inside a correctly encoded field, quote characters appear only in pairs, apart from the opening and closing ones at the edges. A single quote in the middle of a field, followed by something other than another quote or a comma, usually means a missing doubling. An odd number of quote characters in a whole field is a warning sign, because a correct field always has an even number.

What the parser does with an unclosed quote

The second worked case is the line "unfinished,ok with no closing quote. The field opens with a quote, so the parser treats everything that follows as content, including the comma and the letters ok, and keeps looking for a closing quote. It reaches the end of the input without finding one and reports an unclosed quote. That is a structural error, and the converter stops with a message rather than guessing where the field was meant to end. In a larger file the damage can be worse. An unclosed quote near the top may swallow commas and line breaks from many following records, until another stray quote happens to close it. The symptoms are rows that merge together, columns that shift, or a final error far from the real mistake.

Finding the real fault in a damaged file

When a parser reports an unclosed quote, the line it names is usually where the file ended, not where the mistake began. Work backward to the last place where the record structure still looked right. Look for a field that contains a quotation mark but is not wrapped, such as 5" pipe (which this converter rejects with a message that a quote must start a field), or for a quoted field in which an interior quote was written once instead of twice. A text editor that highlights quote pairs, or a tool that counts quotes per line, narrows the search. If a field legitimately spans lines, a line with an odd quote count is not necessarily wrong, so check the pair that continues on the next line. Recover the original field boundary, then fix the quoting. Adding a closing quote at the end of the file may make the error disappear and leave the data wrong.

Commas and line breaks use the same mechanism

Quoting exists for three characters: the comma, the double quote and the line break. A field such as "Smith, Jane" keeps its comma because it is wrapped. A field that contains a line break must also be wrapped, and the parser keeps the break as part of the value. The converter handles records ended with carriage return and line feed as well as plain line feeds, as RFC 4180 describes. Quoting a field that does not need it is allowed, so "ok" parses the same as ok. This is why some exporters quote every field. It is harmless, although it makes the file larger. What is not allowed is leaving a comma or a quote unprotected in an unquoted field, because the parser has no way to tell it apart from structure.

Backslash escaping is a different convention

Many programmers expect a backslash to escape a quote, as in Say \"hello\". Some tools and database exports do write CSV that way, and some parsers have an option for it. The format in RFC 4180 does not use it, and this converter follows the doubled-quote rule. Feed it backslash-escaped text and it reads the quote after the backslash as the end of the field. The next character is then not a comma, so it stops with an error that only a delimiter may follow a closing quote. If a file came from a system that uses backslashes, convert the escaping first or use a parser configured for that dialect. The same caution applies in reverse. If you write CSV for an unknown reader, doubling quotes is the form that spreadsheet programs and standard libraries accept, so it is the safer choice. If you must accept both styles, normalize the file before parsing and verify a few records by eye, because a global replacement of backslash-quote with doubled quotes can damage legitimate backslashes in paths.

Spaces around quotes

Whitespace next to a quote causes quiet trouble. In a strict reading, a quoted field begins at the first character after a comma, so a line such as a, "b" has a field that starts with a space and then a quote. That is not a quoted field, because a quote may only open a field. The same applies after a closing quote: anything between the closing quote and the next comma, including a space, is invalid. The Csv To Json Converter rejects both layouts with an error. Some lenient parsers trim the spaces, but others do not, so files that rely on trimming break when moved to another tool. Write the comma directly against the quote on both sides. A pretty-printed layout with aligned columns is a poor fit for CSV for this reason.

Header mode and where field names come from

The worked cases turn header mode off, so the first record is data and the output is a list of rows. With header mode on, the first record supplies the keys, and each later record becomes an object. The key names are read through the same quoting rules, so a header such as "Quote ""Q""" would produce a key containing literal quotation marks. The converter expects the keys to be unique and rejects duplicates, since an object cannot hold two values under one name. When you convert, decide whether the first row is a header before parsing. A first row that is actually data will be treated as keys, and the real header will be lost in the middle of the rows.

Writing CSV safely from your own data

If you generate CSV, apply the rules mechanically to every value. Replace each double quote with two double quotes, then wrap the value in double quotes if it contains a comma, a quote or a line break. Use a library when you can, since libraries cover edge cases such as empty values and non-ASCII text. One further hazard has nothing to do with parsing. Spreadsheet programs may interpret a cell that starts with an equals sign, plus, minus or at sign as a formula, and OWASP documents this as CSV injection. If a file will be opened in a spreadsheet and contains text from untrusted sources, consider prefixing such cells with an apostrophe or a space. That protects the reader and does not change how a parser sees the quoting.

What the converter does and does not check

The Csv To Json Converter parses structure. It checks that quotes balance, splits records and fields correctly, and optionally builds objects from a header. It does not decide whether a value is a number or a date, it does not repair a file whose delimiter is a semicolon or a tab, and it does not guess intent when quoting is wrong. Regional settings sometimes use the semicolon as the list separator because the comma is the decimal mark, so a file exported from such a system may look like one column. Confirm the delimiter before blaming the quotes. When the quote rules and the delimiter are both right, the parsed rows should match the original data exactly, which is the property to check after every conversion.

A multi-record file with a header

Consider a small file with a header row and two records. The first line is name,note. The second is the record for Ada, whose note is the quoted text She said, then a doubled-quote pair around hi. The third line opens a quoted note for Lin that reads Line one, and the fourth line finishes it with Line two and the closing quote, so the note contains a line break. A person counting lines would see four, but a parser sees three records, because the break inside the quotes belongs to the value. With header mode on, the converter produces two objects. The first has name Ada and a note that contains the words She said "hi" with literal quotes, and the second has name Lin and a note spanning two lines. Row counts that differ between a text editor and a parser are a classic symptom of quoted line breaks, and they are expected, not a sign of corruption.

Reproduce the checks

Literal quotation marks

Open the Csv To Json Converter and enter:

CSV input
"Say ""hello""",ok
First row contains headers
false

Expected output:

Rows

[
  [
    "Say \"hello\"",
    "ok"
  ]
]

The parsed first field contains ordinary literal quotes.

Unclosed quoted field

Open the Csv To Json Converter and enter:

CSV input
"unfinished,ok
First row contains headers
false

Expected rejection:

unclosed quote

A missing closing quote is a structural error.

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