Skip to content

Top 10 JQ Commands Every Linux Developer Should Know

Advertisement

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:

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

  1. Pretty Print JSON Data
  2. To pretty print JSON data, simply pass the JSON file to the JQ command followed by a period (.):

    jq '.' input.json 
    

  3. Retrieve a Specific JSON Key
  4. To extract the value of a specific key from a JSON object, use the following syntax:

    For example, to extract the value of the “name” key, use:

    jq '.name' input.json 
    

  5. Access Nested JSON Values
  6. To access nested JSON values, use the dot (.) notation:

    For example, to extract the value of the “city” key nested under “address”, use:

    jq '.address.city' input.json 
    

  7. Iterate Over JSON Arrays
  8. 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 
    

  9. Filter JSON Data
  10. To filter JSON data based on specific conditions, use the select function:

    For example, to filter objects in an array with an “age” greater than 30, use:

    jq '.[] | select(.age > 30)' input.json 
    

  11. Map and Transform JSON Data
  12. To map and transform JSON data, use the curly braces ({}):

    For example, to create a new JSON object with only the “name” and “age” keys, use:

    jq '.[] | {name: .name, age: .age}' input.json 
    

  13. Combine Multiple JSON Files
  14. To merge two JSON files, use the * operator:

    jq -s '.[0] * .[1]' file1.json file2.json 
    

  15. Perform Arithmetic Operations
  16. JQ can perform arithmetic operations on numeric JSON values:

    For example, to calculate the sum of two numbers in a JSON file, use:

    jq '.number1 + .number2' input.json 
    

  17. Sort JSON Data
  18. To sort JSON data based on a specific key, use the `sort_by` function:

    For example, to sort an array of objects by the “age” key, use:

    jq '.[] | sort_by(.age)' input.json 
    

  19. Group JSON Data
  20. To group JSON data based on a specific key, use the `group_by` function:

    For 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.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top