CSV Rows to JSON Records

Medium ⏱ 12 min 56% acceptance ★★★★★ 4.5
Write csv_to_json(text) where the first line of text is a comma-separated header and following lines are rows: return the json.dumps of the list of dicts mapping headers to cell values (all values kept as strings). Empty trailing lines are ignored.

Examples

Example 1
Input
csv_to_json('name,city\nAsha,Pune\nRaj,Delhi')
Output
[{"name": "Asha", "city": "Pune"}, {"name": "Raj", "city": "Delhi"}]
Explanation

Header keys pair with each row.

Constraints

  • No csv module — split manually.
  • Values stay strings.

Topics

JSONformat conversion

Companies

DeloitteEYKPMG

Hints

Hint 1

zip(headers, row_cells) builds each record.

Hint 2

dict(zip(...)) then dumps the list.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.