我有一个json文件,想用jq解析它,以便在输出中显示如下内容:
.issues.keys .project.self .fixVersions.id .fixVersions.name
.fixVersions.description .resolution.description .resolution.name .versions.name .summary.summary
这是json部分:
[
{
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 56,
"issues": [
{
"key": "AXL-2766",
"fields": {
"issuetype": {
"self": "https://jira/rest/api/2/issuetype/1",
"description": "desc.",
},
"project": {
"self": "https://jira/rest/api/2/project/15770",
"avatarUrls": {
"48x48": "https://jira/secure/projectavatar?avatarId=15683",
}
},
"fixVersions": [
{
"self": "https://jira/rest/api/2/version/18167",
"description": "description1",
"name": "0.2.0",
}
],
"resolution": {
"self": "https://jira/rest/api/2/resolution/1",
"description": "description2",
"name": "Fixed"
},
"watches": {
"self": "https://jira/rest/api/2/issue/AXL-2766/watchers",
},
"created": "2019-03-04T01:10:07.000-0500",
"priority": {
"self": "https://jira/rest/api/2/priority/2",
},
"customfield_10420": null,
"customfield_12322": {
"self": "https://jira/rest/api/2/customFieldOption/10644",
},
"versions": [
{
"self": "https://jira/rest/api/2/version/18165",
"id": "18165",
"description": "",
"name": "0.0"
}
],
"issuelinks": [],
"assignee": {
"emailAddress": "aaa@domain.test"
},
"displayName": "user",
},
"updated": "2019-03-04T05:47:13.000-0500",
"status": {
"self": "https://jira/rest/api/2/status/5",
"description": "description3.",
"statusCategory": {
"colorName": "green",
"name": "Done"
}
},
"summary": "summary",
"creator": {
"self": "https://jira/rest/api/2/user?username=user",
"emailAddress": "user.@domain.net"
},
"subtasks": [],
"reporter": {
"self": "https://jira/rest/api/2/user?username=user",
"emailAddress": "user.@domain.net",
},
"aggregateprogress": {
"progress": 1800,
},
"customfield_10122": null,
"progress": {
"progress": 1800,
},
"votes": {
"self": "https://jira/rest/api/2/issue/AXL-2766/votes",
}
}
}
]
我已经尝试过,但是一直坚持下去:
。[] | {issues:.issues,字段:[.fields .fixVersions .name]}
P.S json文件可能已损坏,因为我已从那里删除了一些私人信息:)
答案 0 :(得分:1)
(假设您的json是固定/有效)
如果您不仅限于使用jq,请允许我为您提供替代解决方案:使用 jtc
unix实用程序,可以轻松遍历json路径(下标看起来像在编程语言,例如类似于Python)。
因此,如果您的json结构是固定的,那么为了转储所有必需的值,请为每个项提供一个遍历路径:
bash $ jtc -w'[0][issues][0][key]' -w'[0][issues][0][fields][project][self]' -w'[0][issues][0][fields][fixVersions][0][name]' -w'[0][issues][0][fields][fixVersions][0][description]' -w'[0][issues][0][fields][resolution][description]' -w'[0][issues][0][fields][resolution][name]' -w'[0][issues][0][summary]' file.json
"AXL-2766"
"https://jira/rest/api/2/project/15770"
"0.2.0"
"description1"
"description2"
"Fixed"
"summary"
bash $
缩短命令:路径[0][issues][0]
的公共部分(每次走动中都存在)可以与可变部分分开,这将产生较短的cli,结果相同:
bash $ jtc -x'[0][issues][0]' -y'[key]' -y'[fields][project][self]' -y'[fields][fixVersions][0][name]' -y'[fields][fixVersions][0][description]' -y'[fields][resolution][description]' -y'[fields][resolution][name]' -y'[summary]' file.json
"AXL-2766"
"https://jira/rest/api/2/project/15770"
"0.2.0"
"description1"
"description2"
"Fixed"
"summary"
bash $
如果您的示例只是更大json的一部分(即未从根目录显示),则将初始部分([0][issues][0]
)替换为使用搜索键的部分:<issues>l[0]
。
您可以在此处找到jtc
使用指南:https://github.com/ldn-softdev/jtc/blob/master/User%20Guide.md