jq - Technological watch

Learn what is jq in less than 5 minutes !
Friday, November 10, 2023

Problematic

Working with JSON files often brings challenges in locating and manipulating specific information. Most modern Linux systems come equipped with three renowned text processing utilities: sed, awk, grep.

Before we delve into the specifics of jq, it is essential to understand the challenges one faces when dealing with JSON data without a specialized tool:

  1. Limited Human Readability: JSON data can be complex and challenging to read, making it hard to discern its structure and content with the naked eye.

  2. Inefficient Manual Parsing: Parsing JSON data manually can be time-consuming and prone to errors.

  3. Limited Transformation Capabilities: Altering or transforming JSON data without a tool like jq often involves writing custom scripts or code, which can be an unnecessary overhead for simple data manipulation tasks.

About the jq tool

jq is a lightweight, flexible, and powerful command-line tool designed for parsing, querying, and transforming JSON data. It provides a concise and expressive way to filter and manipulate JSON, making it an indispensable tool for anyone working with JSON on a regular basis.

Installation

Installing jq is straightforward and varies depending on your operating system. Here are the installation instructions for popular platforms:

  1. Linux (Debian/Ubuntu):
Terminal window
sudo apt-get install jq
  1. Linux (Fedora/RHEL):
Terminal window
sudo yum install jq
  1. macOS (via Homebrew):
Terminal window
brew install jq

Key jq Commands

Now that you have jq installed, let’s explore some key commands that will help you tackle common JSON data manipulation tasks:

  1. Basic JSON Parsing:
Terminal window
jq '.' file.json

or

Terminal window
echo '[{"name":"Alexandre","age":24},{"name":"Hugo","age":25},{"name":"Vincent","age":23}]' | jq '.'
# Output
# [
# { "name": "Alexandre", "age": 24 },
# { "name": "Hugo", "age": 25 },
# { "name": "Vincent", "age": 23 }
# ]

This command will pretty-print the JSON file, making it more human-readable. Replace ’.’ with specific selectors to extract only the data you need.

  1. Filtering JSON Objects:
Terminal window
jq '.key' file.json

Replace .key with the specific key you want to extract from the JSON data.

  1. Iterating over Arrays:
Terminal window
jq '.array[]' file.json

Use this command to extract all elements from a JSON array.

  1. Complex Queries:
Terminal window
jq '.items[] | select(.price < 20)' file.json

This example filters items with a price less than 20 from a JSON array.

  1. Modifying JSON Data:
Terminal window
jq '.key = "new_value"' file.json

This command can be used to update the value of a specific key.

More information

  • 🔄 “jq” is a powerful tool for working with JSON data, enabling you to query, transform, and process JSON in a more efficient and user-friendly way.

  • ⚡ With simple installation steps and key commands at your disposal, you can harness the full potential of “jq” to enhance your data manipulation capabilities.

  • 🎯🚀 Whether you are a developer, data analyst, or system administrator, “jq” is a valuable addition to your toolkit, simplifying JSON data processing and making your work more productive and less error-prone.

More informations here:


Recommended articles