Search code examples
jsonshelljqbitbucket-api

Extract a property slug from a json file


I would like to extract the names of the repositories and their size after getting the list of all the repositories in bitbucket using API via shell script. The command I'm using for that is

repo_list=$(cat repo.json | jq '.[] | .slug ' | sed 's/"//g')

repo.json contains:

{
"pagelen":100,
"size":494,
"values":[
  {
     "scm":"git",
     "website":"",
     "fork_policy":"no_public_forks",
     "full_name":"org_name/ecomm-dist-cache",
     "name":"ecomm-dist-cache",
     "language":"java",
     "created_on":"2014-11-18T19:01:25.741787+00:00",
     "mainbranch":{
        "type":"branch",
        "name":"master"
     },
     "workspace":{
        "slug":"org_name",
        "type":"workspace",
        "name":"Org Name ",
        "uuid":"{xxxxxxxxxxxxx}"
     },
     "has_issues":true,
     "updated_on":"2018-06-06T22:17:02.947496+00:00",
     "size":105095621,
     "type":"repository",
     "slug":"ecomm-dist-cache",
     "is_private":true,
     "description":"Initial Migration of ecomm-dist-cache"
  },
  {
     "scm":"git",
     "website":"",
     "full_name":"org_name/mqfte_ecommoutboundtransfertoweddingchannel",
     "name":"MQFTE_ECOMMOutboundTransferToWeddingChannel",
     "language":"",
     "mainbranch":{
        "type":"branch",
        "name":"master"
     },
     "workspace":{
        "slug":"org_name",
        "type":"workspace",
        "name":"Org Name ",
        "uuid":"{xxxxxxxxxxxxx}"
     },
     "has_issues":false,
     "size":99549,
     "type":"repository",
     "slug":"mqfte_ecommoutboundtransfertoweddingchannel",
     "is_private":true,
     "description":""
  }
],
"page":1,
"next":"https://api.bitbucket.org/2.0/repositories/org_name? pagelen=100&page=2"
}

The error msg I'm getting is

Cannot index number with string "slug"

Expected result is

ecomm-dist-cache
mqfte_ecommoutboundtransfertoweddingchannel

Solution

  • jq -r '.values[].slug' repo.json
    

    -r will remove the quotation marks and so there is no need to pipe through to sed.