Convert tabular CSV file content into structured JSON objects.
CSV (Comma-Separated Values) seems like the simplest format in the world: just split strings by commas. However, a naive string.split(',') algorithm fails immediately in the real world.
What if a user's address is "123 Main St, Apt 4"? If you blindly split by commas, you just broke one column into two, corrupting the entire row's data mapping.
To fix the comma problem, the CSV standard dictates that if a field contains a comma (or a newline), the entire field must be wrapped in double quotes: "123 Main St, Apt 4".
But what if the field itself contains a quote? E.g., Bob "The Builder" Smith. In CSV, internal quotes are escaped by doubling them up: "Bob ""The Builder"" Smith".
Because of these recursive escaping rules, you cannot reliably parse CSV with Regular Expressions.
Robust CSV parsers (like the one powering this tool) use a State Machine. The parser iterates through the file character-by-character, keeping track of its current state (e.g., isInsideQuotes = true). When it sees a comma, it only splits the column if isInsideQuotes is false. This is computationally heavier than a regex split, but guarantees 100% data integrity.
Why does a naive 'string.split(',')' approach fail when parsing real-world CSV files?