0

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")) '
1
  • 3
    Please add your desired output for that sample input to your question. Commented Apr 29, 2019 at 4:11

1 Answer 1

1

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?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.