How To Transform JSON Data with jq?
One of the most popular tools for working with JSON is jq, a command-line tool that is designed to manipulate and transform JSON data. In this article, we will take a look at how to transform JSON data with jq.
Installing jq
jq is a command-line tool that can be installed on Linux, macOS, and Windows. To install jq on your system, follow these steps:
- For Linux: Use your package manager to install jq (e.g.,
apt-get install jq
on Debian/Ubuntu,yum install jq
on CentOS/Red Hat). - For macOS: Install jq using Homebrew (
brew install jq
) or MacPorts (port install jq
). - For Windows: Download the jq executable from the official website and add it to your system path.
Basic Usage
Before we dive into the various transformations that can be performed with jq, let’s take a look at some basic usage examples.
Selecting Specific Data
One of the most common operations with jq is selecting specific data from JSON files. Suppose we have the following JSON file:
{ "name": "John", "age": 30, "email": "john@example.com" }
To select only the “name” field, we can use the following command:
$ cat data.json | jq '.name' "John"
Filtering Data
Another common operation is filtering data based on certain criteria. Suppose we have the following JSON file:
[ {"name": "John", "age": 30}, {"name": "Jane", "age": 25}, {"name": "Bob", "age": 40} ]
To filter only the elements where the age is greater than or equal to 30, we can use the following command:
$ cat data.json | jq '.[] | select(.age >= 30)' { "name": "John", "age": 30 } { "name": "Bob", "age": 40 }
Transforming Data
jq can also be used to transform data in various ways. Suppose we have the following JSON file:
{ "employees": [ {"name": "John", "age": 30}, {"name": "Jane", "age": 25}, {"name": "Bob", "age": 40} ] }
To transform the data by adding a new field called “salary” to each employee, we can use the following command:
$ cat data.json | jq '.employees[] |= . + {"salary": 50000}' { "name": "John", "age": 30, "salary": 50000 } { "name": "Jane", "age": 25, "salary": 50000 } { "name": "Bob", "age": 40, "salary": 50000 }
Advanced Transformations
Now that we’ve covered some basic transformations, let’s take a look at some more advanced ones.
Combining Objects
jq can be used to combine multiple JSON objects into a single object. Suppose we have the following JSON files:
{ "name": "John", "age": 30 }
{ "email": "john@example.com", "phone": "555-1234" }
To combine these two objects into a single object, we can use the following command:
$ jq -s '.[0] * .[1]' file1.json file2.json { "name": "John", "age": 30, "email": "john@example.com", "phone": "555-1234" }
Sorting Data
jq can also be used to sort JSON arrays based on specific criteria. Suppose we have the following JSON file:
[ {"name": "John", "age": 30}, {"name": "Jane", "age": 25}, {"name": "Bob", "age": 40} ]
To sort the array by age in ascending order, we can use the following command:
$ cat data.json | jq 'sort_by(.age)' [ { "name": "Jane", "age": 25 }, { "name": "John", "age": 30 }, { "name": "Bob", "age": 40 } ]
Grouping Data
jq can also be used to group JSON objects based on specific criteria. Suppose we have the following JSON file:
[ {"name": "John", "age": 30}, {"name": "Jane", "age": 25}, {"name": "Bob", "age": 40}, {"name": "Alice", "age": 30} ]
To group the objects by age, we can use the following command:
$ cat data.json | jq 'group_by(.age)' [ [ { "name": "Jane", "age": 25 } ], [ { "name": "John", "age": 30 }, { "name": "Alice", "age": 30 } ], [ { "name": "Bob", "age": 40 } ] ]
jq is a powerful tool for working with JSON data. With its simple yet flexible syntax, jq can be used to perform a wide range of operations on JSON files, including selecting specific data, filtering data, and transforming data. Whether you are a developer or a data analyst, jq is an essential tool to have in your toolbox. I hope this article has been helpful in getting you started with jq.