使用 jq 解析 JIRA Insights API JSON
Parsing JIRA Insights API JSON using jq
所以我基本上有来自 JIRA Insights API 的 JSON 输出,一直在挖掘并找到用于解析 JSON 的 jq。努力思考如何将以下内容解析为我感兴趣的 objectTypeAttributeId 的 return 值。
例如,我只对 objectTypeAttributeId 887 的值感兴趣,前提是 objectTypeAttributeId 911 的名称状态为活动状态,但我想 return 另一个 objectTypeAttributeId
的名称值
仅使用 jq 可以实现吗?或者我应该使用其他东西吗?
我可以过滤到 JSON 输出的 'attributes' 部分并打印每个值,但很难找到适合我情况的示例。
{
"id": 137127,
"objectTypeAttributeId": 887,
"objectAttributeValues": [
{
"value": "false"
}
],
"objectId": 9036,
"position": 16
},
{
"id": 137128,
"objectTypeAttributeId": 888,
"objectAttributeValues": [
{
"value": "false"
}
],
"objectId": 9036,
"position": 17
},
{
"id": 137296,
"objectTypeAttributeId": 911,
"objectAttributeValues": [
{
"status": {
"id": 1,
"name": "Active",
"category": 1
}
}
],
"objectId": 9036,
"position": 18
},
Can this be achieved using jq only?
是的,jq 正是为这种查询而设计的。在您的情况下,您可以使用 any
、select
和 if ... then ... else ... end
,如下所示:
if any(.[]; .objectTypeAttributeId == 911 and
any(.objectAttributeValues[]; .status.name == "Active"))
then map(select(.objectTypeAttributeId == 887))
else "whatever"
end
所以我基本上有来自 JIRA Insights API 的 JSON 输出,一直在挖掘并找到用于解析 JSON 的 jq。努力思考如何将以下内容解析为我感兴趣的 objectTypeAttributeId 的 return 值。
例如,我只对 objectTypeAttributeId 887 的值感兴趣,前提是 objectTypeAttributeId 911 的名称状态为活动状态,但我想 return 另一个 objectTypeAttributeId
的名称值仅使用 jq 可以实现吗?或者我应该使用其他东西吗?
我可以过滤到 JSON 输出的 'attributes' 部分并打印每个值,但很难找到适合我情况的示例。
{
"id": 137127,
"objectTypeAttributeId": 887,
"objectAttributeValues": [
{
"value": "false"
}
],
"objectId": 9036,
"position": 16
},
{
"id": 137128,
"objectTypeAttributeId": 888,
"objectAttributeValues": [
{
"value": "false"
}
],
"objectId": 9036,
"position": 17
},
{
"id": 137296,
"objectTypeAttributeId": 911,
"objectAttributeValues": [
{
"status": {
"id": 1,
"name": "Active",
"category": 1
}
}
],
"objectId": 9036,
"position": 18
},
Can this be achieved using jq only?
是的,jq 正是为这种查询而设计的。在您的情况下,您可以使用 any
、select
和 if ... then ... else ... end
,如下所示:
if any(.[]; .objectTypeAttributeId == 911 and
any(.objectAttributeValues[]; .status.name == "Active"))
then map(select(.objectTypeAttributeId == 887))
else "whatever"
end