Convert JSON to CSV with jq

jq is a lightweight JSON command line processor that makes many JSON processing tasks very easy. A common task when working with JSON is to convert records into a CSV format. Below are examples of how to convert JSON to CSV with jq.

Converting JSON to CSV

In order to convert JSON to CSV with jq, the input data must be in a JSON array. If the array doesn’t already exist it can be built with jq.

This example will take all the items in myarray and convert them into a comma delimited format using the @csv syntax.

Here is a complete example with a sample JSON input:

If you would like to create a CSV from fields that are not in an array, then you can create the array with jq before converting to CSV. The following example will create a CSV from the key1 field and the first element in myarray:

Enclosing Fields in Quotes

By default each CSV record as well as all its fields will be enclosed in quotes. The -r option used in the examples above will remove the outer quotes from the output.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">