Convert CSV to JSON with Header Keys, Quoted Fields and Malformed Rows

Read the full interview experience this question came from →

Quick Overview

A practical coding question: convert CSV text with a header row into a JSON array of objects. It tests a quote-aware parser that handles commas, doubled quotes and line breaks inside fields, correct JSON escaping, clear errors for malformed rows, type handling and streaming large files.

Convert CSV to JSON with Header Keys, Quoted Fields and Malformed Rows

Company: Scale AI

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Onsite

In a practical coding round, write a converter from CSV to JSON. The input is CSV text whose first line is a header naming the columns. Every following record becomes one JSON object that maps each column name to that record's value, and the output is a JSON array of those objects. The report names only the task, so agree on the parsing and output rules before coding: CSV has edge cases that break a naive implementation. ```python def csv_to_json(csv_text: str) -> str: ... ``` Illustrative input: ```text id,name,city 1,Ana,"Austin, TX" 2,"Li ""Leo"" Chen", ``` Expected output: ```json [ {"id": "1", "name": "Ana", "city": "Austin, TX"}, {"id": "2", "name": "Li \"Leo\" Chen", "city": ""} ] ``` ```hint A comma is not always a separator Decide how the parser knows whether it is inside a quoted field, and what that means for the commas, quote characters and line breaks it meets there. ``` ```hint Escaping goes both ways Values can contain characters that are special in JSON as well as in CSV. Decide which layer is responsible for escaping them in the output. ``` ### Clarifying Questions - Should values stay strings, or should numbers, booleans and empty fields be converted (to numbers, `true`/`false`, `null`)? - What should happen when a row has more or fewer fields than the header, or when header names repeat or are empty? - Can quoted fields contain commas, doubled quotes and line breaks? Are line endings `\n`, `\r\n` or both, and can the file start with a byte-order mark? - Is the input small enough to hold in memory, or must the converter stream a large file? - Should each object keep the header's column order, and should the output be one JSON array or one object per line? ### What a Strong Answer Covers - Settles typing, malformed-row and output-shape rules before coding - A quote-aware parser that handles commas, doubled quotes and line breaks inside quoted fields, and both line-ending styles - Correct JSON produced by a serializer, with key order preserved and proper escaping - Clear errors for malformed input (an unterminated quote, a row with the wrong number of fields) that include the line number - Tests for the edge cases, and a plan for streaming large files ### Follow-up Questions - The file is too large to fit in memory. How do you convert it? - Infer column types (integers, decimals, booleans, nulls) consistently per column rather than per cell. What rules do you use, and which values must stay strings? - Header names such as `address.city` should become nested objects. How do you build them, and what conflicts can occur? - Would you use your own parser or a standard library parser in production, and what would you test before trusting either?

Overview: A practical coding question: convert CSV text with a header row into a JSON array of objects. It tests a quote-aware parser that handles commas, doubled quotes and line breaks inside fields, correct JSON escaping, clear errors for malformed rows, type handling and streaming large files.

Read the full Scale AI Software Engineer interview experience this question came from

Scale AI logo
Scale AI
Jul 9, 2026
easySoftware EngineerOnsiteSoftware Engineering Fundamentals
1
0

In a practical coding round, write a converter from CSV to JSON. The input is CSV text whose first line is a header naming the columns. Every following record becomes one JSON object that maps each column name to that record's value, and the output is a JSON array of those objects. The report names only the task, so agree on the parsing and output rules before coding: CSV has edge cases that break a naive implementation.

def csv_to_json(csv_text: str) -> str:
    ...

Illustrative input:

id,name,city
1,Ana,"Austin, TX"
2,"Li ""Leo"" Chen",

Expected output:

[
  {"id": "1", "name": "Ana", "city": "Austin, TX"},
  {"id": "2", "name": "Li \"Leo\" Chen", "city": ""}
]

Clarifying Questions Guidance

  • Should values stay strings, or should numbers, booleans and empty fields be converted (to numbers, true / false , null )?
  • What should happen when a row has more or fewer fields than the header, or when header names repeat or are empty?
  • Can quoted fields contain commas, doubled quotes and line breaks? Are line endings \n , \r\n or both, and can the file start with a byte-order mark?
  • Is the input small enough to hold in memory, or must the converter stream a large file?
  • Should each object keep the header's column order, and should the output be one JSON array or one object per line?

What a Strong Answer Covers Guidance

  • Settles typing, malformed-row and output-shape rules before coding
  • A quote-aware parser that handles commas, doubled quotes and line breaks inside quoted fields, and both line-ending styles
  • Correct JSON produced by a serializer, with key order preserved and proper escaping
  • Clear errors for malformed input (an unterminated quote, a row with the wrong number of fields) that include the line number
  • Tests for the edge cases, and a plan for streaming large files

Follow-up Questions Guidance

  • The file is too large to fit in memory. How do you convert it?
  • Infer column types (integers, decimals, booleans, nulls) consistently per column rather than per cell. What rules do you use, and which values must stay strings?
  • Header names such as address.city should become nested objects. How do you build them, and what conflicts can occur?
  • Would you use your own parser or a standard library parser in production, and what would you test before trusting either?