JQ Examples: Manipulating and Querying JSON Data


JSON (JavaScript Object Notation) is a popular data format used for storing and exchanging data. It's widely supported and commonly used in web applications and APIs. When working with JSON data, you might find yourself needing to manipulate or query the data in various ways. That's where JQ comes in handy.

JQ is an incredibly powerful command-line tool designed for processing JSON data. Its lightweight nature and expressive query language make it an excellent choice for manipulating and querying JSON structures. In this blog post, we will delve into several practical examples that demonstrate the versatility of JQ in manipulating and querying JSON data. With JQ, you can effortlessly extract, filter, and transform JSON data to suit your specific needs. Join us as we explore the capabilities of JQ and uncover its potential for working with JSON structures.

./jq

Installation

Before we dive into the examples, let's ensure that JQ is installed on your system. Here are some quick installation instructions:

macOS (Homebrew)

shell:    brew install jq

Ubuntu/Debian

shell:    sudo apt-get update
              sudo apt-get install jq

Fedora/CentOS

shell:    sudo dnf install jq

Windows

To install JQ on Windows, you can follow these steps:

  1. Visit the JQ releases page on GitHub: https://github.com/stedolan/jq/releases.
  2. Scroll down to the "Assets" section and locate the Windows version of JQ. It will be a .exe file.
  3. Download the latest version of JQ for Windows by clicking on the .exe file.
  4. Once the download is complete, locate the downloaded .exe file and run the installer.
  5. Follow the instructions provided by the installer to complete the installation process.
  6. After the installation is finished, you can open a command prompt and use the jq command to start using JQ.

Now that you have JQ installed on your system, you can proceed with the examples and explore the power of JQ for manipulating and querying JSON data.

Example JSON Data

Let's start with a simple example JSON document that represents a list of employees:

{
"employees": [
{
"name": "John Doe",
"age": 30,
"skills": ["Python", "JavaScript", "SQL"],
"projects": [
{
"name": "Project A",
"role": "Developer"
},
{
"name": "Project B",
"role": "Manager"
}
]
},
{
"name": "Jane Smith",
"age": 25,
"skills": ["Java", "C#", "HTML"],
"projects": [
{
"name": "Project C",
"role": "Developer"
},
{
"name": "Project D",
"role": "Tester"
}
]
}
]
}

Now, let's explore some JQ examples to manipulate and query this JSON data.

Querying Data

JQ provides a powerful query language to extract specific data from JSON structures. Here are some examples:

Example 1: List all employee names

jq '.employees[].name' employees.json

Output

"John Doe"
"Jane Smith"

Example 2: Filter employees with age greater than 28

jq '.employees[] | select(.age > 28)' employees.json

Output

{
"name": "John Doe",
"age": 30,
"skills": ["Python", "JavaScript", "SQL"],
"projects": [
{
"name": "Project A",
"role": "Developer"
},
{
"name": "Project B",
"role": "Manager"
}
]
}

Example 3: Retrieve the skills of all employee

jq '.employees[].skills' employees.json

Output

["Python", "JavaScript", "SQL"]
["Java", "C#", "HTML"]

Example 4: Get the names of projects for each employee

jq '.employees[].projects[].name' employees.json

Output

"Project A"
"Project B"
"Project C"
"Project D"

Manipulating Data

In addition to querying data, JQ also allows you to manipulate JSON structures. Here are some examples:

Example 5: Update an employee's age

jq '.employees[0].age = 32' employees.json

Output

{
"employees": [
{
"name": "John Doe",
"age": 32,
"skills": ["Python", "JavaScript", "SQL"],
"projects": [
{
"name": "Project A",
"role": "Developer"
},
{
"name": "Project B",
"role": "Manager"
}
]
},
{
"name": "Jane Smith",
"age": 25,
"skills": ["Java", "C#", "HTML"],
"projects": [
{
"name": "Project C",
"role": "Developer"
},
{
"name": "Project D",
"role": "Tester"
}
]
}
]
}

Example 6: Add a new skill to an employee

jq '.employees[1].skills += ["CSS"]' employees.json

Output

{
"employees": [
{
"name": "John Doe",
"age": 30,
"skills": ["Python", "JavaScript", "SQL"],
"projects": [
{
"name": "Project A",
"role": "Developer"
},
{
"name": "Project B",
"role": "Manager"
}
]
},
{
"name": "Jane Smith",
"age": 25,
"skills": ["Java", "C#", "HTML", "CSS"],
"projects": [
{
"name": "Project C",
"role": "Developer"
},
{
"name": "Project D",
"role": "Tester"
}
]
}
]
}

Example 7: Remove a project from an employee

jq 'del(.employees[0].projects[0])' employees.json

Output

{
"employees": [
{
"name": "John Doe",
"age": 30,
"skills": ["Python", "JavaScript", "SQL"],
"projects": [
{
"name": "Project B",
"role": "Manager"
}
]
},
{
"name": "Jane Smith",
"age": 25,
"skills": ["Java", "C#", "HTML"],
"projects": [
{
"name": "Project C",
"role": "Developer"
},
{
"name": "Project D",
"role": "Tester"
}
]
}
]
}

These examples demonstrate the power and flexibility of JQ when it comes to manipulating and querying JSON data. By leveraging its simple and expressive language, you can efficiently extract, filter, transform, and update JSON structures according to your specific needs.

Feel free to experiment with more complex JSON data and explore the extensive capabilities of JQ. Happy querying and manipulating!