JSON to CSV in Linux
Online CSV to JSON converter: https://commabot.com/csv-to-json
Converting JSON to CSV is relatively straightforward and can be done with a command line tool like jq.
For a conversion where you have a simple JSON array of objects, jq
can be used directly to format the data as CSV. Consider the following JSON file named data.json
:
[
{
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com"
},
{
"name": "Jane Doe",
"age": 25,
"email": "janedoe@example.com"
}
]
To convert this to CSV, you can use the following jq
command:
jq -r '.[] | [.name, .age, .email] | @csv' data.json > data.csv
This command breaks down as follows:
-r
: Outputs raw strings, not JSON texts..[]
: Iterates over each element in the JSON array.[.name, .age, .email]
: Creates a new array with the specified fields for each element.@csv
: Formats the array as a CSV line.
The output data.csv
will look like this:
"John Doe",30,"johndoe@example.com"
"Jane Doe",25,"janedoe@example.com"