skip to Main Content

GitLab allows one to attach labels to issues. I would like to use jq to dig into the JSON representation of these labels. Here’s the labels section of the JSON file for one issue:

"labels": [
   "AI::Assessed",
   "Pri::1 - High",
   "Type::Investigation",
   "v::Backlog"
]

How, with jq, can I get at the value of the "v" label, which here is "Backlog"?

Note that the number of labels varies from issue to issue. The :: signifies that a label is a scoped label.

2

Answers


  1. You can use capture :

    #!/bin/bash
    
    echo '{"labels": [
       "AI::Assessed",
       "Pri::1 - High",
       "Type::Investigation",
       "v::Backlog"
    ]}' | jq -r '.labels[] | capture("v::(?<v>.*)").v'
    
    # Output : Backlog
    
    Login or Signup to reply.
  2. A trivial solution with startswith and substring/string-slicing:

    .labels[] | select(startswith("v::"))[3:]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search