Unable to parse json output of artifactory with jq in linux

1.4k views Asked by At

After executing this query in Artifactory

/usr/bin/jfrog rt s foo/01_Develop/01_CI/HPCC-Package-*.zip

I have this output:

[Info] Searching artifacts...
[Info] Found 3 artifacts.
[
  {
    "path": "foo/01_Develop/01_CI/HPCC-Package-47.zip"
  },
  {
    "path": "foo/01_Develop/01_CI/HPCC-Package-48.zip"
  },
  {
    "path": "foo/01_Develop/01_CI/HPCC-Package-72.zip"
  }
]

I want to get the last path in json array with this command as suggested here:

/usr/bin/jfrog rt s foo/01_Develop/01_CI/HPCC-Package-*.zip | jq .[-1].path

But fails with

parse error: Invalid numeric literal at line 1, column 6

I cannot change json as it is the output from artifactory jfrog tool

  • How can I fix JQ query?
  • Is there any other way to get the last path?

NOTE: I have jq version 1.5

UPDATE:

Using quotes I have the exact same error:

/usr/bin/jfrog rt s foo/01_Develop/01_CI/HPCC-Package-*.zip | jq '.[-1].path'
/usr/bin/jfrog rt s foo/01_Develop/01_CI/HPCC-Package-*.zip | jq ".[-1].path"
3

There are 3 answers

0
peak On BEST ANSWER

For the record, here's a jq-only solution that assumes there are exactly two lines of non-JSON prolog:

... | jq -n -R -r '[inputs][2:] | join("") | fromjson[-1]' 
{
  "path": "foo/01_Develop/01_CI/HPCC-Package-72.zip"
}
0
Jeff Mercado On

Your Artifactory output isn't pure json... you need to remove those non-json parts. Assuming we will only need to skip the first two lines, we could just use tail to skip em.

/usr/bin/jfrog rt s foo/01_Develop/01_CI/HPCC-Package-*.zip | tail -n +3 | jq '.[-1].path'
0
Dima Nevelev On

As mentioned in previous answers, this error occurred because the output of JFrog CLI is not pure JSON.
You may want to set the JFROG_CLI_LOG_LEVEL environment variable to ERROR, so that additional messages will not be prompted by the command.
For more details you can read JFrog CLI wiki.