jq - Technological watch
Problematic
Working with JSON files often presents challenges when it comes to locating and manipulating data. While most modern Linux systems offer powerful text processing tools like sed, awk, and grep, using them to parse JSON can be error-prone. And when it comes to modifying JSON content, the process can become especially difficult.
About the jq tool
jq is a lightweight 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.
Installation
Installing jq is straightforward and varies depending on your operating system. Here are the installation instructions for popular platforms:
- Linux (Debian/Ubuntu):
sudo apt-get install jq- Linux (Fedora/RHEL):
sudo yum install jq- macOS (via Homebrew):
brew install jqKey jq commands
Now that you have jq installed, let’s explore some key commands that will help you tackle common JSON data manipulation tasks:
- Basic JSON parsing:
jq '.' file.jsonor
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.
- Filtering JSON objects:
jq '.key' file.jsonReplace .key with the specific key you want to extract from the JSON data.
- Iterating over arrays:
jq '.array[]' file.jsonUse this command to extract all elements from a JSON array.
- Complex queries:
jq '.items[] | select(.price < 20)' file.jsonThis example filters items with a price less than 20 from a JSON array.
- Modifying JSON data:
jq '.key = "new_value"' file.jsonThis command can be used to update the value of a specific key.
More information
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:
- Official website: https://jqlang.github.io/jq/
- Manual: https://jqlang.github.io/jq/manual/
- Small guide to use jq: https://www.baeldung.com/linux/jq-command-json
Atomic design - Technological watch
Learn what is the atomic design in less than 5 minutes !
Redis - Technological watch
Learn what is Redis in less than 5 minutes !
Svelte - Technological watch
Learn what is Svelte in less than 5 minutes !
Jenkins - Technological watch
Learn what is Jenkins in less than 5 minutes !
PicoCSS - Technological watch
Learn what is PicoCSS in less than 5 minutes !
OpenAPI / Swagger - Technological watch
Learn what is OpenAPI / Swagger in less than 5 minutes !
RabbitMQ - Technological watch
Learn what is RabbitMQ in less than 5 minutes !
Falco - Technological watch
Learn how to protect your Kubernetes cluster in less than 5 minutes !