How to parse JSON string via command line on Linux

If you often deal with JSON-formatted texts from the command line or in shell scripts, you may wonder if there is any command-line utility which can parse JSON string. A command-line JSON parser can be handy when you test or debug JSON web services. You can feed JSON-formatted responses from web services into the command-line JSON parser, thereby easily inspecting otherwise hard-to-read JSON responses or extracting individual objects from them.

In this tutorial, I will describe how to parse JSON string from the command line.

On Linux, there is a command-line JSON processor called jq which does exactly that. Using jq, you can parse, filter, map, and transform JSON-structured data effortlessly.

To install jq on Linux, simply download its binary (available for 32-bit and 64-bit system separately) as follows.

$ wget (32-bit system)
$ wget (64-bit system)
$ chmod +x ./jq
$ sudo cp jq /usr/bin

The jq binary is also available for Windows and OS X platforms, and its full source code is released under the MIT license.

The following examples illustrate how to parse JSON-structured data with jq.

An example JSON Schema:

$ cat json.txt
        "name": "Google",
                        "street": "1600 Amphitheatre Parkway",
                        "city": "Mountain View",
                        "state": "California",
                        "country": "US"
                                "name": "Michael",
                                "division": "Engineering"
                                "name": "Laura",
                                "division": "HR"
                                "name": "Elise",
                                "division": "Marketing"

To parse a JSON object:

$ cat json.txt | jq '.name'

To parse a nested JSON object:

$ cat json.txt | jq ''
"Mountain View"

To parse a JSON array:

$ cat json.txt | jq '.employees[0].name'

To extract specific fields from a JSON object:

$ cat json.txt | jq '.location | {street, city}'
  "city": "Mountain View",
  "street": "1600 Amphitheatre Parkway"

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

11 thoughts on “How to parse JSON string via command line on Linux

  1. What to do if your json is:
    "city_state": "oxitz1tom01-RedHat-NO Oracle"

    .location.contry.city_state do not work

  2. 'sudo cp jq /usr/bin'
    please never alter contents of your /usr/bin manually. it may mess with package system and therefore is potentially harmful. use /usr/local/bin for that

    • Late but an answer :)
      you can count the objects in an array this way:

      cat json.txt | jq '.array[].maybe_wanted_key' | wc -l

      to count keys with a known content put a grep into the pipe:
      cat json.txt | jq '.array[].maybe_wanted_key' | grep 'what_you_are_looking_for' | wc -l

  3. We use a lot of hyphenated keys, and the default


    was not working. But this did work:


  4. Thank you very much, but I want to add some new example.

            "category": "tag_mei_jia", 
            "tagPrice": [
                    "activeTagDictId": 370, 
                    "activePrice": 39
                    "activeTagDictId": 371, 
                    "activePrice": 68
                    "activeTagDictId": 372, 
                    "activePrice": 99

    I want to get all activeTagDictId list:
    $ cat test.json | jq '.[].tagPrice[].activeTagDictId'

    I want to get activePrice value when activeTagId is a specific value:
    $ cat test.json | jq '.[].tagPrice[]|select(.activeTagDictId==372)|.activePrice'

Leave a comment

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