How to parse JSON string via command line on Linux

Last updated on October 20, 2020 by Dan Nanni

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.

Install jq on Linux

Install jq on Ubuntu, Debian or Linux Mint

$ sudo apt-get install jq

Install jq on CentOS or RHEL

First, enable EPEL repository and run:

$ sudo yum install jq

Instll jq on any other Linux

You can simply download its binary (available for 32-bit and 64-bit system separately) as follows.

On 64-bit system:

$ wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
$ chmod +x ./jq-linux64
$ sudo cp jq-linux64 /usr/bin/jq

On 32-bit system, replace the above wget command with the following.

$ wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux32

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

Parse JSON String with jq

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

An example JSON Schema:

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

To parse a JSON object:

$ cat json.txt | jq '.name'
"Google"

To parse a nested JSON object:

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

To parse a JSON array:

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

To extract specific fields from a JSON object:

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

If you want to parse JSON string inside your program (e.g., written in Python or Perl), you can refer to these Python and Perl tutorials. If what you are interested in is to check if a given JSON string conforms to JSON schema, this tutorial may be useful.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean