Tools
Get Pro

Convert JSON to CSV with jq

@csv turns an array into one CSV line, quoting strings and escaping commas correctly. Run it with -r, or you get each line wrapped in JSON quotes. Prefer clicking? The JSON to CSV converter does this without a filter.

One row per item

Build an array of the cell values for each item and pipe it to @csv.

.users[] | [.id, .name, .country] | @csvRaw outputTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

1,"Ada","UK"
2,"Linus","FI"
3,"Grace","US"
jq -r '.users[] | [.id, .name, .country] | @csv' data.json

Add a header row

Output the header array first, then the rows. The comma runs both.

["id","name","country"], (.users[] | [.id, .name, .country]) | @csvRaw outputTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

"id","name","country"
1,"Ada","UK"
2,"Linus","FI"
3,"Grace","US"
jq -r '["id","name","country"], (.users[] | [.id, .name, .country]) | @csv' data.json

Header from the keys

For flat objects, take the column names from the first item so you never type them twice.

(.[0] | keys_unsorted) as $cols | $cols, (.[] | [.[$cols[]]]) | @csvRaw outputTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

"name","age","team"
"Ada",36,"core"
"Linus",28,"kernel"
"Grace",45,"core"
"Ken",28,"kernel"
jq -r '(.[0] | keys_unsorted) as $cols | $cols, (.[] | [.[$cols[]]]) | @csv' data.json

Arrays and nested values in a cell

A cell must be a string, number, boolean or null. Join arrays and pick nested fields first.

.users[] | [.name, (.tags | join(";")), (.orders | length)] | @csvRaw outputTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

"Ada","admin;dev",2
"Linus","dev",0
"Grace","dev;ops",1
jq -r '.users[] | [.name, (.tags | join(";")), (.orders | length)] | @csv' data.json

Tab-separated: @tsv

.[] | [.name, .age] | @tsvRaw outputTry it

Input

[
  {
    "name": "Ada",
    "age": 36,
    "team": "core"
  },
  {
    "name": "Linus",
    "age": 28,
    "team": "kernel"
  },
  {
    "name": "Grace",
    "age": 45,
    "team": "core"
  },
  {
    "name": "Ken",
    "age": 28,
    "team": "kernel"
  }
]

Output

Ada	36
Linus	28
Grace	45
Ken	28
jq -r '.[] | [.name, .age] | @tsv' data.json

On the command line

jq -r '.users[] | [.id, .name, .country] | @csv' users.json > users.csv

Common mistakes

  • Forgetting -r: every line comes out as a JSON string like "1,\"Ada\",\"UK\"".
  • "object cannot be csv-formatted": a cell is an object or array. Pick a field or join it.

Practice

Open the jq Playground with your own JSON — it runs the real jq 1.8 in your browser, so nothing is uploaded. The jq cheat sheet lists every filter from these lessons on one page.