Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 13, 2023
Invariably, there is an inevitable need to convert data between various formats during data manipulation and analysis. In particular, we often encounter the task of converting JSON (JavaScript Object Notation) to CSV (Comma-Separated Values) in Linux. However, this can be achieved using several Linux commands.
In this tutorial, we’ll understand the importance of this conversion and explore the different methods to accomplish it using the Linux command-line tools.
To begin, let’s see why it’s necessary to convert JSON to CSV:
Next, let’s explore the different methods for converting JSON to CSV using Linux commands.
jq is a simple and powerful command-line JSON processor in Linux. In fact, it allows parse, filter, and manipulate JSON data.
Let’s use the below data.json as an input file throughout this article:
$ cat data.json
{
"employees": [
{
"name": "Alice",
"department": "HR",
"age": 28
},
{
"name": "Bob",
"department": "IT",
"age": 32
}
]
}
Now, let’s use jq to convert JSON to CSV:
$ jq -r '.employees[] | [.name, .department, .age] | @csv' data.json > data.csv
Here,
Subsequently, we save the resulting CSV file as data.csv:
$ cat data.csv
name,department,age
Alice,HR,28
Bob,IT,32
Further, we can adjust the JSON path to match the data structure. Also, jq is a good choice for data processing in high-performance environments. jq efficiently processes large data sets by reducing network latency and improving user experience.
Alternatively, we can use awk, a versatile text processing tool in Linux, to convert JSON to CSV. It scans the file line by line and splits it into multiple fields.
Let’s say we have a JSON file named data.json:
$ awk -F '[:,]' 'BEGIN { print "name,department,age" } \
/"name"/ { gsub(/[" ]/, "", $2); name = $2 } \
/"department"/ { gsub(/[" ]/, "", $2); department = $2 } \
/"age"/ { gsub(/[" ]/, "", $2); age = $2;
print name "," department "," age }' data.json > data.csv
Here,
Lastly, let’s save the output as data.csv.
$ cat data.csv
name,department,age
Alice,HR,28
Bob,IT,32
In some cases, we may need to customize this command to match the structure of our JSON data.
Sometimes, we might not have jq or awk installed on our Linux system. In such cases, Python is a versatile alternative to convert JSON to CSV.
Let’s see how to use a Python script to achieve this:
$ python3 -c "import json, csv; \
data = json.load(open('data.json')); \
employees = data['employees']; \
with open('data.csv', 'w', newline='') as csvfile: \
csv.writer(csvfile).writerow(['name', 'department', 'age']); \
[csv.writer(csvfile).writerow([e['name'], e['department'], e['age']]) for e in employees]"
Consequently, this command loads the JSON data, extracts the keys, and writes them to a CSV file named data.csv:
$ cat data.csv
name,department,age
Alice,HR,28
Bob,IT,32
Let’s remember that this command requires Python on our Linux system.
Analogously, another method to convert JSON to CSV is to use a combination of sed and paste commands. Basically, the sed command is a powerful text stream editor in Linux, widely used for text manipulation. In this case, we can use sed to clean up the JSON data and remove unnecessary characters such as curly braces and commas.
Now, let’s see how to use the sed command:
$ sed -n '/"name":/s/^[[:space:]]*"name": "\(.*\)",$/\1/gp;/"department":/s/^[[:space:]]*"department": "\(.*\)",$/\1/gp;/"age":/s/^[[:space:]]*"age": \([0-9]*\)$/\1/gp' data.json | paste -d, - - -
Here,
This is a substitution command that extracts the value associated with “name” from the line. It looks for the “name” key and captures its value within double quotes.
In this manner, the combination of sed and paste cleans up and transforms the JSON data into a CSV format.
Another method is dasel, a command-line utility that manipulates data in various formats, including JSON. Fundamentally, dasel simplifies this conversion with a user-friendly command structure.
Now, let’s explore how to convert JSON to CSV using the dasel command:
$ dasel -i data.json to csv -a employees -p name,department,age
name,department,age
Alice,HR,28
Bob,IT,32
Here:
As we can see, whether we need to extract, transform, or convert data, dasel is a valuable addition to our data toolkit.
In conclusion, this article portrays how converting JSON to CSV in a Linux environment is a valuable skill when working with different data formats and tools. In addition, we learned to use Linux commands such as jq, awk, sed, tr, and dasel to perform this conversion efficiently without any additional software or online tools.
Hence, the choice of method ultimately depends on our specific requirements and the tools available on the system.