What does jq do

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: jq is a lightweight and flexible command-line JSON processor. It allows you to easily slice, filter, map, and transform structured data in JSON format, similar to how `sed`, `awk`, or `grep` work for text.

Key Facts

Overview

In the realm of command-line tools and data processing, handling structured data efficiently is paramount. JavaScript Object Notation (JSON) has become a ubiquitous format for data interchange, especially in web APIs and configuration files. However, raw JSON output can often be verbose and difficult to navigate or extract specific information from. This is where jq, a powerful and flexible command-line JSON processor, comes into play. Think of jq as a specialized tool for working with JSON data directly from your terminal, offering capabilities akin to text-processing utilities like sed, awk, and grep, but specifically tailored for the hierarchical structure of JSON.

jq enables users to easily slice, filter, map, and transform JSON data. Whether you need to extract a single value from a complex nested object, select specific elements from an array, reformat the entire JSON structure, or perform calculations on the data, jq provides a concise and expressive query language to achieve these tasks. Its lightweight nature and speed make it an indispensable tool for developers, system administrators, and anyone who frequently interacts with JSON data in a command-line environment.

What is jq?

jq is an open-source, lightweight, and flexible command-line JSON processor. Created by Stephen Dolan, it was first released in 2012. Its primary purpose is to allow users to easily parse, filter, transform, and manipulate JSON data from the command line. It operates by taking JSON input, applying a specified jq filter (a query written in jq's own domain-specific language), and producing the processed JSON output.

Key Features and Capabilities

jq offers a rich set of features designed for comprehensive JSON manipulation:

1. Data Extraction and Filtering

One of jq's most common uses is to extract specific pieces of information from JSON data. You can access nested fields using dot notation (e.g., .user.name) or array elements by index (e.g., .items[0]). Filters can also be combined to create more complex queries. For example, to get the names of all users from a JSON array of user objects, you might use a filter like .[] | .name.

2. Data Transformation and Reshaping

jq excels at reshaping JSON data. You can create new JSON objects or arrays based on the input data. This is incredibly useful for reformatting API responses to match the expected structure of another system or for simplifying data for easier consumption. For instance, you could transform an array of user objects into an object where the keys are user IDs and the values are user names.

3. Data Manipulation and Calculation

Beyond simple extraction, jq can perform operations on the data. This includes arithmetic operations, string concatenation, conditional logic (using if-then-else statements), and working with dates and times. This makes it possible to perform data cleaning and basic analysis directly within the command line.

4. Input and Output Flexibility

jq can read JSON from standard input (stdin), from files, or directly from a string. Similarly, its output can be directed to standard output (stdout), files, or piped to other command-line tools. This seamless integration with other Unix-like tools makes it a powerful component in shell scripting and automation pipelines.

5. Pretty-Printing and Formatting

By default, jq often pretty-prints JSON, making it more human-readable than minified JSON. You can also use jq to explicitly format JSON output, controlling indentation and other stylistic elements.

Common Use Cases

jq finds extensive application in various scenarios:

Getting Started with jq

To use jq, you first need to install it. Installation methods vary depending on your operating system (e.g., using package managers like apt, yum, brew, or downloading binaries). Once installed, you can run jq commands from your terminal.

A basic jq command structure looks like this:

echo '' | jq ''

or

jq ''

For example, if you have a file named data.json with the content:

{ "name": "Alice", "age": 30, "city": "New York" }

To extract just the name, you would run:

cat data.json | jq '.name'

This would output:

"Alice"

If you wanted to get the name and age as a new object:

cat data.json | jq '{ personName: .name, personAge: .age }'

This would output:

{ "personName": "Alice", "personAge": 30 }

Conclusion

jq is an indispensable tool for anyone working with JSON data on the command line. Its powerful query language, combined with its flexibility and efficiency, makes complex JSON manipulation tasks straightforward and manageable. By mastering jq, you can significantly enhance your productivity when dealing with structured data in command-line environments.

Sources

  1. jq ManualMIT
  2. Jq (software) - WikipediaCC-BY-SA-4.0
  3. JSON - MDN Web DocsCC-BY-SA-2.5

Missing an answer?

Suggest a question and we'll generate an answer for it.