Convert JSON to TSV 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 TSV (tab delimited) format. Below are examples of how to convert JSON to TSV with jq.

Converting JSON to TSV

In order to convert JSON to TSV 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 tab delimited format using the @tsv syntax.

Here is a complete example with a sample JSON input:

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

Enclosing Fields in Quotes

By default each tab delimited record 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="">