JQ is a versatile command-line JSON processor for Linux that allows developers to parse, filter, and transform JSON data quickly and efficiently. Its powerful features make it an indispensable tool for anyone working with JSON files on Linux systems. In this article, we’ll cover the top 10 essential JQ commands that every Linux developer should know to streamline their JSON processing tasks.
Example JSON File Content
For this tutorial, you can use the following example JSON data in a file named input.json. This JSON data represents an array of people with their respective names, ages, countries, and addresses:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | [ { "name": "Alice", "age": 35, "country": "USA", "address": { "street": "123 Main St", "city": "New York", "state": "NY", "zip": "10001" } }, { "name": "Bob", "age": 28, "country": "Canada", "address": { "street": "456 Maple Ave", "city": "Toronto", "province": "ON", "postal_code": "M5V 1A1" } }, { "name": "Charlie", "age": 42, "country": "USA", "address": { "street": "789 Oak St", "city": "San Francisco", "state": "CA", "zip": "94102" } }, { "name": "David", "age": 23, "country": "Canada", "address": { "street": "321 Pine St", "city": "Vancouver", "province": "BC", "postal_code": "V6B 2P4" } } ] |
Save this JSON content in a file named “input.json” and use it as the input for the various JQ commands in the tutorial. This sample data will help you better understand and practice the different JQ commands and techniques outlined in the article.
10 JQ Command Examples for Linux Developers
- Pretty Print JSON Data
To pretty print JSON data, simply pass the JSON file to the JQ command followed by a period (
.
):jq '.' input.json
- Retrieve a Specific JSON Key
To extract the value of a specific key from a JSON object, use the following syntax:
1jq '.key' input.jsonFor example, to extract the value of the “name” key, use:
jq '.name' input.json
- Access Nested JSON Values
To access nested JSON values, use the dot (
.
) notation:1jq '.key1.key2.key3' input.jsonFor example, to extract the value of the “city” key nested under “address”, use:
jq '.address.city' input.json
- Iterate Over JSON Arrays
Use the square brackets (
[]
) to iterate over JSON arrays:jq '.[]' input.json
To extract specific keys from each object within the array, use the pipe (
|
) operator:jq '.[] | .key' input.json
- Filter JSON Data
To filter JSON data based on specific conditions, use the select function:
1jq '.[] | select(.key == "value")' input.jsonFor example, to filter objects in an array with an “age” greater than 30, use:
jq '.[] | select(.age > 30)' input.json
- Map and Transform JSON Data
To map and transform JSON data, use the curly braces (
{}
):1jq '.[] | {key1: .key1, key2: .key2}' input.jsonFor example, to create a new JSON object with only the “name” and “age” keys, use:
jq '.[] | {name: .name, age: .age}' input.json
- Combine Multiple JSON Files
To merge two JSON files, use the
*
operator:jq -s '.[0] * .[1]' file1.json file2.json
- Perform Arithmetic Operations
JQ can perform arithmetic operations on numeric JSON values:
1jq '.number1 + .number2' input.jsonFor example, to calculate the sum of two numbers in a JSON file, use:
jq '.number1 + .number2' input.json
- Sort JSON Data
To sort JSON data based on a specific key, use the `sort_by` function:
1jq '.[] | sort_by(.key)' input.jsonFor example, to sort an array of objects by the “age” key, use:
jq '.[] | sort_by(.age)' input.json
- Group JSON Data
To group JSON data based on a specific key, use the `group_by` function:
1jq 'group_by(.key)' input.jsonFor example, to group an array of objects by the “country” key, use:
jq 'group_by(.country)' input.json
Conclusion
These top 10 essential JQ commands offer a solid foundation for Linux developers working with JSON files. Mastering these commands will help you parse, filter, and manipulate JSON data more efficiently, ultimately enhancing your development workflow.
As you gain experience with JQ, you’ll discover even more advanced features and techniques that cater to your specific needs. The JQ command-line tool is an invaluable asset for any developer working with JSON data on Linux systems, and knowing these essentials will serve as a strong starting point in your journey to becoming a JQ power user.
To further expand your JQ knowledge, consider diving into the official JQ documentation (https://stedolan.github.io/jq/manual/) and exploring community resources, such as tutorials, blog posts, and forums. As you continue to sharpen your JQ skills, you’ll unlock the full potential of this powerful JSON processing tool, making it an integral part of your Linux development toolkit.