8

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
Learner
  • 105
  • 1
  • 1
  • 7
  • 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 Sep 17 '19 at 11:33
  • ...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. – Charles Duffy Sep 17 '19 at 11:34

2 Answers2

7

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.

Bayou
  • 3,293
  • 1
  • 9
  • 22
  • 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 https://artifactory.es.abc.com/artifactory/api/storage/abc/com/xyz/aa/bb/xx/?list&listFolders=1&includeRootPath=0 > tmp.json 3rd Line is - grep 'uri' tmp.json | sed -r 's/^[^:]*:(.*)$/\1/' My data is like- { "uri" : "/abc", "folder" : true }, – Learner Sep 17 '19 at 09:59
  • echo '{ "uri" : "/abc", "folder" : true },' | grep -Eo '"uri"[^,]*' | grep -Eo '[^:]*$' worked for me.. Thanks :) – Learner Sep 17 '19 at 10:17
7

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}'
Prateek Sen
  • 392
  • 2
  • 9
  • 3
    `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 – Charles Duffy Sep 17 '19 at 11:32
  • `awk -F'"' '/uri/{print $(NF-1)}' process.json` – NanoNova Aug 11 '23 at 02:40