Ausfestivus
Ausfestivus

Reputation: 25

select object by nested field's value and extract a first level field

I have the following json:

[
  {
    "uuid": "rvbrc7y6a5d6xpz37av534zv3a",
    "templateUuid": "101",
    "trashed": "N",
    "createdAt": "2018-07-26T05:48:51Z",
    "updatedAt": "2018-07-27T00:26:35Z",
    "changerUuid": "Y7WX2RXJ35GCLLERQA2T74CKNM",
    "itemVersion": 3,
    "vaultUuid": "awn3jry3oo55xdcfgs7bnlyioa",
    "overview": {
      "ps": 0,
      "title": "Env Vars - CSP 1"
    }
  },
  {
    "uuid": "xtf3vdsnw5ardldytwh6edihzu",
    "templateUuid": "001",
    "trashed": "N",
    "createdAt": "2013-10-15T01:29:57Z",
    "updatedAt": "2016-11-22T20:57:47Z",
    "changerUuid": "Y7WX2RXJ35GCLLERQA2T74CKNM",
    "itemVersion": 1,
    "vaultUuid": "qmorozyspqhb26kr4z7nbt7hpy",
    "overview": {
      "URLs": [
        {
          "u": "https://example.com"
        }
      ],
      "ainfo": "*******@gmail.com",
      "ps": 72,
      "title": "https://example.com",
      "url": "https://example.com"
    }
  }
]

There are another ~500 objects in the array which I need to parse. Three of the objects have an .overview.title which contains the string "Env Vars".

Im trying to use jq to get the output to look like:

"rvbrc7y6a5d6xpz37av534zv3a", "Env Vars - CSP 1"
"another uuid", "Env Vars - CSP 2"
"yet another uuid", "Env vars - CSP n"

Ive tried a few different things including:

And a lot of different variations of the above. This is the first time Ive had to use jq in anger so it's pretty new to me. The jq doco wasn't very helpful tbh.

Would like to buy a clue please.

Upvotes: 1

Views: 273

Answers (1)

oguz ismail
oguz ismail

Reputation: 50795

Select objects where .overview.title contains Env Vars, extract .uuid out of them, make arrays of UUID and title pairs, and feed them to @csv filter.

.[]
| .overview.title as $t 
| select($t | index("Env Vars")) 
| [.uuid, $t]
| @csv

Note that you need to specify -r/--raw-output flag on the command line for jq to yield valid CSV.

Upvotes: 1

Related Questions