Jay Myers
Jay Myers

Reputation: 9

Parsing a JSON array with jq

I need to parse out specific information from the following JSON example bellow using jq. depending on the 'repo' I need to be able to parse from the properties array all 'key' & 'values' and associated with the repository. For example the repo "libs-production-local" would need to parse out any properties key with 'prod' in the string and its associated date value. has to be jq and run in .sh.

{
   "results":[
      {
         "repo":"libs-production-local",
         "path":"com/company/version",
         "name":"sql.21.tar",
         "type":"file",
         "size":"40123",
         "created":"date",
         "created_by":"someone",
         "modified":"date",
         "modified_by":"someone",
         "updated":"date",
         "depth":4,
         "actual_md5":"asdflsdf23a4324234",
         "orginal_sha1":"sadlkfjsdklfjsadf",
         "properties":[
            {
               "key":"deploy.uat",
               "value":"2018-09-23"
            },
            {
               "key":"deploy.prod.TLE",
               "value":"2018-10-20"
            },
            {
               "key":"deploy.prodXYZ",
               "value":"2018-10-20"
            },
            {
               "key":"deploy.prodPDQ",
               "value":"2018-10-20"
            },
            {
               "key":"deploy.prod.ABC",
               "value":"2018-10-21"
            },
            {
               "key":"businessUnit.name",
               "value":"IndivdualName"
            },
            {
               "key":"deploy.qa.ser2",
               "value":"2018-10-20"
            },
            {
               "key":"deploy.qa.ser1",
               "value":"2018-11-23"
            },
            {
               "key":"build.timestamp",
               "value":"1510850899004"
            }
         ],
         "virtual_repos":[
            "libs-production "
         ]
      },
      {
         "repo":"libs-production-local",
         "path":"com/company/version",
         "name":"sql.22.tar",
         "type":"file",
         "size":"40123",
         "created":"date",
         "created_by":"someone",
         "modified":"date",
         "modified_by":"someone",
         "updated":"date",
         "depth":4,
         "actual_md5":"asdflsdf23a4324234",
         "orginal_sha1":"sadlkfjsdklfjsadf",
         "properties":[
            {
               "key":"deploy.prodPDQ",
               "value":"2018-10-22"
            },
            {
               "key":"deploy.prodABC",
               "value":"2018-10-20"
            },
            {
               "key":"businessUnit.name",
               "value":"IndivdualName"
            },
            {
               "key":"deploy.qa",
               "value":"2018-10-20"
            },
            {
               "key":"deploy.dev",
               "value":"2018-11-19"
            }
         ],
         "virtual_repos":[
            "libs-production "
         ]
      }
   ],
   "range":{
      "start_pos":0,
      "end_pos":479,
      "total":479
   }
}

I've tried a number of ways to do this (including this one) and nothing works.

jq -r '.results[] |  ( .properties |map(select(.key[] contains ("prod")) '

Upvotes: 0

Views: 10934

Answers (1)

drosboro
drosboro

Reputation: 457

I solved it like this:

jq -r '[ .results[].properties[] | select(.key | contains("prod")) ]'

This grabs all key-value pairs from each result's properties array. It then selects those that contain "prod" in the key, and returns an array of those keys and values. Given your example input from above, this is the return value:

[
  {
    "key": "deploy.prod.TLE",
    "value": "2018-10-20"
  },
  {
    "key": "deploy.prodXYZ",
    "value": "2018-10-20"
  },
  {
    "key": "deploy.prodPDQ",
    "value": "2018-10-20"
  },
  {
    "key": "deploy.prod.ABC",
    "value": "2018-10-21"
  },
  {
    "key": "deploy.prodPDQ",
    "value": "2018-10-22"
  },
  {
    "key": "deploy.prodABC",
    "value": "2018-10-20"
  }
]

Is that close to what you're looking for?

Upvotes: 1

Related Questions