Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»JQ Command in Linux with Examples

    JQ Command in Linux with Examples

    RahulBy RahulAugust 13, 20215 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.

    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.

    In this post, we will learn 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 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:

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

    We can use the cat command to show the data:

    cat employee.json
    

    cat command example

    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
    

    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 property 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
    

    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
    

    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:

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

    To output all the arrays present in the JSON file we will run the command given below:

    jq '.[]' employee.json
    

    To output only the second array we can modify the above-given command in the following way:

    jq '.[1]' employee.json
    

    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
    

    Similarly, to access all the name properties inside arrays we can execute this command:

    jq '.[].name' employee.json 
    

    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
    Previous ArticleHow To Install PIP in Ubuntu 20.04
    Next Article Chown Command in Linux with Examples

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    1 Comment

    1. Indrajeet on December 29, 2021 10:37 am

      Helpful information.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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