'Json file to get out properties

So I have this json output that could be like this. Could be more could be less:

{
"values": [
{
  "id": "9207968154959e84a61207773ba595bab4e09a74",
  "displayId": "92079681549",
  "author": {
    "name": "ADBE08",
    "emailAddress": "[email protected]",
    "id": 30405,
    "displayName": "Adam svensson",
    "active": true,
    "slug": "adbe08",
    "type": "NORMAL"
  },
  "authorTimestamp": 1650961815000,
  "committer": {
    "name": "ADBE08",
    "emailAddress": "[email protected]",
    "id": 30405,
    "displayName": "Adam svensson",
    "active": true,
    "slug": "adbe08",
    "type": "NORMAL"
  },
  "committerTimestamp": 1650961815000,
  "message": "fixes sonar issues",
  "parents": [
    {
      "id": "fe037562528e899c2e76c42cd261d227f0884f81",
      "displayId": "fe037562528"
    }
  ]
},


{
  "id": "9b722800273e421ec4a5add0a4cc624ca04347f0",
  "displayId": "9b722800273",
  "author": {
    "name": "ADBE08",
    "emailAddress": "[email protected]",
    "id": 30405,
    "displayName": "Adam svensson",
    "active": true,
    "slug": "adbe08",
    "type": "NORMAL"
  },
  "authorTimestamp": 1650622775000,
  "committer": {
    "name": "ADBE08",
    "emailAddress": "[email protected]",
    "id": 30405,
    "displayName": "Adam svensson",
    "active": true,
    "slug": "adbe08",
    "type": "NORMAL"
  },
  "committerTimestamp": 1650622775000,
  "message": "removes profiles for testing purpose",
  "parents": [
    {
      "id": "1f1adad1754b064aba630f7df3a8f196dc4478fe",
      "displayId": "1f1adad1754"
    }
  ]
},
{
  "id": "1f1adad1754b064aba630f7df3a8f196dc4478fe",
  "displayId": "1f1adad1754",
  "author": {
    "name": "ADBE08",
    "emailAddress": "[email protected]",
    "id": 30405,
    "displayName": "Adam svensson",
    "active": true,
    "slug": "adbe08",
    "type": "NORMAL"
  },
  "authorTimestamp": 1650620887000,
  "committer": {
    "name": "ADBE08",
    "emailAddress": "[email protected]",
    "id": 30405,
    "displayName": "Adam svensson",
    "active": true,
    "slug": "adbe08",
    "type": "NORMAL"
  },
  "committerTimestamp": 1650620887000,
  "message": "Merge remote-tracking branch 'origin/feat-KOKO-8243-csca-dd-priv-some-create-purchase-for-test' into feat-debug-menu",
  "parents": [
    {
      "id": "01332cb1374c34aeb2a91b69029f4f4072ea147d",
      "displayId": "01332cb1374"
    },
    {
      "id": "8e36ec4b3aacda03b5e52bc8df0c43d44f49c72e",
      "displayId": "8e36ec4b3aa"
    }
  ],
  "properties": {
    "jira-key": [
      "KOKO-8243"
    ]
  }
},
{
  "id": "8e36ec4b3aacda03b5e52bc8df0c43d44f49c72e",
  "displayId": "8e36ec4b3aa",
  "author": {
    "name": "niol05",
    "emailAddress": "[email protected]",
    "id": 27253,
    "displayName": "anna Olsson",
    "active": true,
    "slug": "niol05",
    "type": "NORMAL"
  },
  "authorTimestamp": 1644848083000,
  "committer": {
    "name": "niol05",
    "emailAddress": "[email protected]",
    "id": 27253,
    "displayName": "anna Olsson",
    "active": true,
    "slug": "niol05",
    "type": "NORMAL"
  },
  "committerTimestamp": 1644848083000,
  "message": "Merge branch 'dev' into feat-KOKO-8243-csca-dd-priv-some-create-purchase-for-test",
  "parents": [
    {
      "id": "633747c469cf488a92f3a29034a47c1f42c1fd81",
      "displayId": "633747c469c"
    },
    {
      "id": "1466e5fbfee12dce35fa21c35c157e9ce934ff9e",
      "displayId": "1466e5fbfee"
    }
  ],
  "properties": {
    "jira-key": [
      "KOKO-8243"
    ]
  }
},

{
  "id": "4b5b85beabb8a7779ab13a5e5de3108103e1cc8b",
  "displayId": "4b5b85beabb",
  "author": {
    "name": "niol05",
    "emailAddress": "[email protected]",
    "id": 27253,
    "displayName": "anna Olsson",
    "active": true,
    "slug": "niol05",
    "type": "NORMAL"
  },
  "authorTimestamp": 1623928598000,
  "committer": {
    "name": "niol05",
    "emailAddress": "[email protected]",
    "id": 27253,
    "displayName": "anna Olsson",
    "active": true,
    "slug": "niol05",
    "type": "NORMAL"
  },
  "committerTimestamp": 1623928598000,
  "message": "Added support for creating test purchase",
  "parents": [
    {
      "id": "7f7da00e46f415fd7f593f0951d04910149ff786",
      "displayId": "7f7da00e46f"
    }
  ]
}
],
"size": 5,
"isLastPage": false,
"start": 0,
"limit": 25,
"nextPageStart": 25
 }

I want to bring out all info in "jira-key". FX "properties":{"jira-key":["KOKO-13230"]} Will bring out this commit in a row. Next jira-key commit in a new row.

fx:

  • KOKO-13230
  • KOKO-12443
  • KOKO-3342
  • KOKO-32142
  • KOKO-334212

How can I do that. I use bash



Solution 1:[1]

You can use jq tool to find all the values.properties.jira-key data. Your example feed to the following command:

jq '.values[].properties?."jira-key"[]?' < example

Will produce:

"KOKO-8243"
"KOKO-8243"

If your json is pretty-printed just like above, you can try also a little bit hackish code using awk:

awk '/]/ { ok=0 } // { if (ok==1) { print $1 } } /"jira-key":/ { ok=1 }' < example

For your example data it should display the same output.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1