user3006778
user3006778

Reputation: 41

How to use jq to obtain the value of a GitLab issue label

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.

Upvotes: 0

Views: 79

Answers (2)

knittl
knittl

Reputation: 265747

A trivial and straightforward solution with startswith and substring/string-slicing:

.labels[] | select(startswith("v::"))[3:]

If you need it to be more dynamic:

jq --arg scope 'v::' '.labels[] | select(startswith($scope))[$scope|length:]'

Upvotes: 0

Philippe
Philippe

Reputation: 26727

You can use scan (or capture) :

#!/bin/bash

echo '{"labels": [
   "AI::Assessed",
   "Pri::1 - High",
   "Type::Investigation",
   "v::Backlog"
]}' | jq -r '.labels[] | scan("v::(.*)")[]'

# Output : Backlog

Upvotes: 0

Related Questions