[jq] Parse a CSV file and convert it to JSON

Created on November 28, 2026

You can use jq's built-in split function to split input at commas and convert it to JSON. The following example shows how to do this.

First, create a file named dummy.csv with the following contents.

1,"Tom",15
2,"Bob",30
3,"Alice",20

Run the following command.

cat dummy.csv | tr -d '"' | jq -R 'split(",") | {"id": .[0] | tonumber, "name": .[1], "age": .[2] | tonumber}'

Explanation

  • tr strips the double quotes from the input so that those quotes do not carry over into the JSON strings.
  • The -R option makes jq read the input as raw text.
  • split(",") parses each CSV row by splitting it at commas.
  • tonumber converts the numeric fields from the CSV to JSON numbers.

Output

{
  "id": 1,
  "name": "Tom",
  "age": 15
}
{
  "id": 2,
  "name": "Bob",
  "age": 30
}
{
  "id": 3,
  "name": "Alice",
  "age": 20
}
jq