10

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
2
  • 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). Commented Sep 17, 2019 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. Commented Sep 17, 2019 at 11:34

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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/aa/… > tmp.json 3rd Line is - grep 'uri' tmp.json | sed -r 's/^[^:]*:(.*)$/\1/' My data is like- { "uri" : "/abc", "folder" : true },
echo '{ "uri" : "/abc", "folder" : true },' | grep -Eo '"uri"[^,]*' | grep -Eo '[^:]*$' worked for me.. Thanks :)
8

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}'

2 Comments

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.
awk -F'"' '/uri/{print $(NF-1)}' process.json

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.