Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»JQ Command in Linux with Examples

    JQ Command in Linux with Examples

    By RahulMarch 24, 20235 Mins Read

    JSON is a data representation format that is used to store and transfer data between different layers of an application; it stores data in key: value pairs.

    Advertisement

    The syntax of JSON was derived from JavaScript but it itself is language-independent. It is compatible with many programming languages; these languages include code that can be used to integrate JSON into the program; but unfortunately, we cannot work with JSON directly in Linux shell as it cannot interpret it. To work with JSON in the Linux shell we use a mixture of tools such as JQ and sed.

    This post will teach us to use the JQ command to manipulate and work with JSON data in a Linux shell.

    How to Install the JQ command

    The JQ command is not available in some Linux distributions by default; It needs to be downloaded into the system before it can be used on the terminal; You can download the JQ command just like any other package on your system. On Ubuntu 20.04 use the below-given command to install the JQ utility:

    sudo apt install jq 
    

    Just replace apt with the package manager of your system if you are running a distribution other than Ubuntu.

    If you are running a distribution like CentOS 8 which already has JQ by default then you will get an output similar to this:

    sudo dnf install jq 
    

    Syntax

    Now we can start using the JQ command as it has been successfully installed on our system, but first, let’s take a look at the syntax of the JQ command:

    jq [options]  [file...]
    
    jq [options] --args  [strings...]
    
    jq [options] --jsonargs  [JSON_TEXTS...]
    

    The JQ command can be used in many different ways; It can be used directly on a JSON file and can also be combined with several other commands to interpret JSON data. The JQ command can be used with different filters such as the “.”, “|”, “,” or the “.[]” filter to organize JSON data.

    The JQ command also takes different options as arguments such as the --tab, --stream, --indent n, --unbuffered, and the -L directory option. The syntax of the JQ command might seem complex at first but you will get familiar with it once you read the whole article.

    How to Organize JSON data using JQ command

    The simplest and most frequently used feature of the JQ command filters. They are used to organize and prettify JSON data when printing it to standard output.

    • In this example, we have a JSON file named employee.json and we need to output the data to the standard output:

      1
      {"workers":{"name": "John Brooks","id": "003"}}

    • We can use the cat command to show the data:
      cat employee.json
      

      JQ Command in Linux with Examples

    • The data printed to the standard output using the cat command is unorganized and messy. We can organize this data by using the JQ command along with the ‘.’ filter:
      jq '.' employee.json
      

      JQ Command in Linux with Examples

    Now the data has become a lot more organized, colorful, and easier to understand. This filter is especially needed when accessing data from APIs; The data stored in APIs can be very unorganized and confusing.

    How to Access a Property using JQ command

    The .field filter along with the JQ command can be used to access object properties in the shell.

    • If we only want to access and print a single property to the standard output then we can use the .field operator. E.g to access the worker’s property we can use this command:
      jq '.workers' employee.json
      

      JQ Command in Linux with Examples

    • We can also access the items present within the property by using the .field operator. To access the name item in the worker’s property we will use:
      jq '.workers.name' employee.json
      

      JQ Command in Linux with Examples

    How to Access an Array Item using JQ command

    We can also access and output the elements present within an array in a JSON file by using the .[] operator. For this example we are going to modify our JSON file so it looks like this:

    1
    [{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]

    1. To output all the arrays present in the JSON file we will run the command given below:
      jq '.[]' employee.json
      

      JQ Command in Linux with Examples

    2. To output only the second array we can modify the above-given command in the following way:
      jq '.[1]' employee.json
      

      JQ Command in Linux with Examples

    3. Remember that the array starts at 0

      We can also access the properties present within the array by using the .field operator. E.g if we want to access the name property in the third array then we will run the following command:

      jq '.[2].name' employee.json
      

      JQ Command in Linux with Examples

    4. Similarly, to access all the name properties inside arrays we can execute this command:
      jq '.[].name' employee.json 
      

      JQ Command in Linux with Examples

    Conclusion

    The JQ command is used to transform JSON data into a more readable format and print it to the standard output on Linux. The JQ command is built around filters which are used to find and print only the required data from a JSON file.

    In this how-to guide, we have learned to use the JQ command to organize and filter JSON data.

    command jq linux
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Top 10 JQ Commands Every Linux Developer Should Know

    How to Check if a Program Exists in Linux

    How to Create a Directory If It Does Not Exist in Linux

    View 2 Comments

    2 Comments

    1. Carlos on September 23, 2022 2:42 pm

      Very good! Helped a lot. It was easily found by Google, looking for exactly what I was looking for and what I found. Thank you.

      Reply
    2. Indrajeet on December 29, 2021 10:37 am

      Helpful information.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.