How to read and parse JSON in shell scripting without using json tool and JQ tool

13,822

Solution 1

You can make use of grep and sed to obtain the data you need. I would have added a full example, but the URL is not correct and you haven't supplied (a part of) the json.

$ cat tmp.json
{
        "json": {
                 "array": [ 1, 2, 3 ]
        },
        "uri": "derp"
}
$ grep 'uri' tmp.json | sed -r 's/^[^:]*:(.*)$/\1/'
 "derp"

For data on a single line, you can make use of the following command:

echo '{ "uri" : "/abc", "folder" : true },' | grep -Eo '"uri"[^,]*' | grep -Eo '[^:]*$'

The first grep will search for "uri" and everything after until a comma is found, the second grep extracts everything from the colon to the end of the string.

Solution 2

The more readable and easy to understand solution would be:

cat process.json | tr { '\n' | tr , '\n' | tr } '\n' | grep "uri" | awk  -F'"' '{print $4}'
Share:
13,822

Related videos on Youtube

Learner
Author by

Learner

Updated on June 04, 2022

Comments

  • Learner
    Learner over 1 year

    I am trying to download artifacts(.war file) from artifactory through rest api. I am getting folder names from artifactory in JSON format and I wanted to get that in one variable through shell script but I am not able to do that without using JSON tool and JQ tool as my server does not contain both of these. Can someone please help me with these.

    I tried below commands

    $ databasename=`cat process.json | json select '.uri'`
    $ echo $databasename
    

    and

    $ databasename=`jq '.uri' process.json`
    

    as I do not have JQ and JSON tool, it failed.

    $ curl -u User:API Key https://artifactory.es.abc.com/artifactory/api/storage/abc/com/xyz/aa/bb/xx/?list&listFolders=1&includeRootPath=0  > process.json
    $ databasename=`cat process.json | json select '.uri'`
    $ echo $databasename
    
    • Charles Duffy
      Charles Duffy about 4 years
      See the answers using python in the linked duplicate. (There are also some awk-based solutions, but they're less capable for the usual reasons).
    • Charles Duffy
      Charles Duffy about 4 years
      ...and note that none of your answers parse JSON in the formal meaning of the word "parse", or the broad meaning of "JSON" as "all documents that comply with the JSON specification". There are lots of corner cases a textual parser will know nothing about -- if something contains \" instead of a literal quote, for example, that needs to turn into a " in the data instead of ending the string it's part of. And string can contain quotes (if thusly quoted), or curly braces... etc.
  • Learner
    Learner about 4 years
    I tried with your solution but getting below error- line 2: {: command not found line 3: uri: command not found 2nd line is- curl -u User:API Key artifactory.es.abc.com/artifactory/api/storage/abc/com/xyz/a‌​a/… > tmp.json 3rd Line is - grep 'uri' tmp.json | sed -r 's/^[^:]*:(.*)$/\1/' My data is like- { "uri" : "/abc", "folder" : true },
  • Learner
    Learner about 4 years
    echo '{ "uri" : "/abc", "folder" : true },' | grep -Eo '"uri"[^,]*' | grep -Eo '[^:]*$' worked for me.. Thanks :)
  • Charles Duffy
    Charles Duffy about 4 years
    awk can do all the work of tr and grep and sed, and cat is never needed when reading only a single file (its job is to concatenate multiple files into a single stream). cat foo | bar gives bar only a FIFO, whereas bar <foo gives it a real file handle for its stdin; that's an especially big deal when bar is something that can benefit from being able to seek() around in its input (f/e, to parallelize I/O operations between different threads, as sort does) or to interrogate the FD to get its length in constant-time instead of needing to read front-to-back.