1

I have a file abc.txt the contents are

{"storageSummary":{"binariesSummary":{"binariesCount":"703","binariesSize":"882.39 MB","artifactsSize":"3.41 GB","optimization":"25.23%","itemsCount":"4,126","artifactsCount":"1,917"},"fileStoreSummary":{"storageType":"file-system","storageDirectory":"/jfrog_uat_nfs/binaries","totalSpace":"1.30 TB","usedSpace":"1.23 GB (0.09%)","freeSpace":"1.30 TB (99.91%)"},"repositoriesSummaryList":[{"repoKey":"sbt_remote-cache","repoType":"CACHE","foldersCount":0,"filesCount":0,"usedSpace":"0 bytes","itemsCount":0,"packageType":"SBT","percentage":"0%"},{"repoKey":"test7.mvlo","repoType":"LOCAL","foldersCount":0,"filesCount":1,"usedSpace":"128 bytes","itemsCount":1,"packageType":"Maven","percentage":"0%"},{"repoKey":"scripttestkp.rplo","repoType":"LOCAL","foldersCount":0,"filesCount":0,"usedSpace":"0 bytes","itemsCount":0,"packageType":"RPM","percentage":"0%"},{"repoKey":"test7.grvr","repoType":"VIRTUAL","foldersCount":0,"filesCount":0,"usedSpace":"0 bytes","itemsCount":0,"packageType":"Gradle","percentage"

From this file how to print only "repoKey" and "usedSpace" in shell script.

abhi
  • 55
  • 1
  • 1
  • 6
  • 1
    Is this a `json` input?, looks like a broken `JSON` input file, and provide the complete `json` input. – Inian Mar 08 '17 at 06:03
  • Use an appropriate `JSON` parser after fixing your input file – Inian Mar 08 '17 at 06:07
  • Why do you say the json is wrong, Inian? Seems okay on first glance... looks like he missed a bit of cut and paste maybe – me_ Mar 08 '17 at 06:12
  • @me_: Try placing the input in https://jsonformatter.curiousconcept.com/, to see if it is syntactically right. It isn't – Inian Mar 08 '17 at 06:13
  • He obviously missed the last bits of the json in cut and paste... beyond that it is correct, typo on thos site not incorrect format... – me_ Mar 08 '17 at 06:16
  • This is big file i copied a small part. Actually this is the output of a curl command. My requirement is from that output i want repoKey filed and usedSpace fileld.{"repoKey":"test7.mvlo","repoType":"LOCAL","foldersCount":0,"filesCount":1,"usedSpace":"128bytes", – abhi Mar 08 '17 at 07:10

1 Answers1

-1

you need to access: data.storageSummary.repositoriesSummaryList[i].repoKey for each i in the list

As well as iterating over everything that has usedSpace as a key... for example: data.storageSummary.fileStoreSummary.usedSpace and data.storageSummary.repositoriesSummaryList[i].usedSpace for each i

using jq in the commandline of linux boxes, the command to access the first instance of usedSpace is:

curl "site" | jq ".storageSummary.fileStoreSummary.usedSpace"

output is:

"1.23 GB (0.09%)"

the command to access the array of values for usedSpace under repoKeys is:

curl "site" | jq ".storageSummary.repositoriesSummaryList[].usedSpace"

the output is:

"0 bytes"
"128 bytes"
"0 bytes"
"0 bytes"

the command to access the array of values of repoKey is:

curl "site" | jq ".storageSummary.repositoriesSummaryList[].repoKey"

the output is:

"sbt_remote-cache"
"test7.mvlo"
"scripttestkp.rplo"
"test7.grvr"

this can be verified by correcting the json provided so that it is valid then heading on over to https://jqplay.org/

me_
  • 681
  • 1
  • 8
  • 18