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.

Advertisement

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

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

    jq '.' input.json 
    
  2. Retrieve a Specific JSON Key

    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 
    
  3. Access Nested JSON Values

    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 
    
  4. 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 
    
  5. Filter JSON Data

    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 
    
  6. Map and Transform JSON Data

    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 
    
  7. Combine Multiple JSON Files

    To merge two JSON files, use the * operator:

    jq -s '.[0] * .[1]' file1.json file2.json 
    
  8. Perform Arithmetic Operations

    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 
    
  9. Sort JSON Data

    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 
    
  10. Group JSON Data

    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.

Share.
Leave A Reply


Exit mobile version