json - 搜索 JSON 文件 bash

标签 json bash

我有以下 JSON 文件,想提取其中的一部分:

  {
    "expand": "names,schema",
    "issues": [
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "fields": {
                "aggregateprogress": {
                    "progress": 0,
                    "total": 0
                },
                "aggregatetimeestimate": null,
                "aggregatetimeoriginalestimate": null,
                "aggregatetimespent": null,
                "assignee": {
                    "active": true,
                    "avatarUrls": {
                        "16x16": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16",
                        "24x24": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24",
                        "32x32": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32",
                        "48x48": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48"
                    },
                    "displayName": "user",
                    "emailAddress": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e590968097a5868a8895848b9ccb868a88" rel="noreferrer noopener nofollow">[email protected]</a>",
                    "key": "user",
                    "name": "user",
                    "self": "https://jira.corp.company.com/rest/api/2/user?username=dvucanovic",
                    "timeZone": "Europe/Belgrade"
                },
                "components": [],
                "created": "2018-03-06T21:24:41.000+0000",
                "creator": {
                    "active": true,
                    "avatarUrls": {
                        "16x16": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16",
                        "24x24": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24",
                        "32x32": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32",
                        "48x48": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48"
                    },
                    "displayName": "user",
                    "emailAddress": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="334640564173505c5e43525d4a1d505c5e" rel="noreferrer noopener nofollow">[email protected]</a>",
                    "key": "user",
                    "name": "user",
                    "self": "https://jira.corp.company.com/rest/api/2/user?username=dvucanovic",
                    "timeZone": "Europe/Belgrade"
                },
                "customfield_10000": null,
                "customfield_10001": null,
                "customfield_10002": null,
                "customfield_10004": "0|i00uta:",
                "customfield_10005": null,
                "customfield_10006": null,
                "customfield_10100": null,
                "customfield_10101": [],
                "customfield_10102": null,
                "customfield_10103": null,
                "customfield_10107": {
                    "id": "10400",
                    "self": "https://jira.corp.company.com/rest/api/2/customFieldOption/10400",
                    "value": "Hentsu Internal"
                },
                "customfield_10108": null,
                "customfield_10200": null,
                "customfield_10201": "2018-03-06",
                "customfield_10202": "2018-03-06",
                "customfield_10203": null,
                "customfield_10204": null,
                "customfield_10205": null,
                "customfield_10206": null,
                "customfield_10300": "com.atlassian.servicedesk.plugins.approvals.internal.customfield.ApprovalsCFValue@5e6792fb",
                "customfield_10301": null,
                "customfield_10302": null,
                "customfield_10600": null,
                "customfield_10700": null,
                "customfield_11000": null,
                "customfield_11001": null,
                "customfield_11002": null,
                "customfield_11003": null,
                "customfield_11004": null,
                "customfield_11005": null,
                "customfield_11006": null,
                "customfield_11007": null,
                "customfield_11008": null,
                "customfield_11009": null,
                "customfield_11010": null,
                "customfield_11011": null,
                "customfield_11012": null,
                "customfield_11013": null,
                "customfield_11014": null,
                "customfield_11015": null,
                "customfield_11016": null,
                "customfield_11017": null,
                "customfield_11018": null,
                "customfield_11019": null,
                "customfield_11100": null,
                "customfield_11101": null,
                "customfield_11102": null,
                "description": null,
                "duedate": null,
                "environment": null,
                "fixVersions": [],
                "issuelinks": [],
                "issuetype": {
                    "avatarId": 10318,
                    "description": "A task that needs to be done.",
                    "iconUrl": "https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
                    "id": "10100",
                    "name": "Task",
                    "self": "https://jira.corp.company.com/rest/api/2/issuetype/10100",
                    "subtask": false
                },
                "labels": [],
                "lastViewed": "2018-03-06T21:31:34.315+0000",
                "priority": {
                    "iconUrl": "https://jira.corp.company.com/images/icons/priorities/medium.svg",
                    "id": "3",
                    "name": "Medium",
                    "self": "https://jira.corp.company.com/rest/api/2/priority/3"
                },
                "progress": {
                    "progress": 0,
                    "total": 0
                },
                "project": {
                    "avatarUrls": {
                        "16x16": "https://jira.corp.company.com/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201",
                        "24x24": "https://jira.corp.company.com/secure/projectavatar?size=small&pid=10001&avatarId=10201",
                        "32x32": "https://jira.corp.company.com/secure/projectavatar?size=medium&pid=10001&avatarId=10201",
                        "48x48": "https://jira.corp.company.com/secure/projectavatar?pid=10001&avatarId=10201"
                    },
                    "id": "10001",
                    "key": "TECH",
                    "name": "Technology",
                    "self": "https://jira.corp.company.com/rest/api/2/project/10001"
                },
                "reporter": {
                    "active": true,
                    "avatarUrls": {
                        "16x16": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16",
                        "24x24": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24",
                        "32x32": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32",
                        "48x48": "https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48"
                    },
                    "displayName": "user",
                    "emailAddress": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="57222432251734383a2736392e7934383a" rel="noreferrer noopener nofollow">[email protected]</a>",
                    "key": "user",
                    "name": "user",
                    "self": "https://jira.corp.company.com/rest/api/2/user?username=dvucanovic",
                    "timeZone": "Europe/Belgrade"
                },
                "resolution": null,
                "resolutiondate": null,
                "status": {
                    "description": "",
                    "iconUrl": "https://jira.corp.company.com/",
                    "id": "10000",
                    "name": "Backlog",
                    "self": "https://jira.corp.company.com/rest/api/2/status/10000",
                    "statusCategory": {
                        "colorName": "blue-gray",
                        "id": 2,
                        "key": "new",
                        "name": "To Do",
                        "self": "https://jira.corp.company.com/rest/api/2/statuscategory/2"
                    }
                },
                "subtasks": [
                    {
                        "fields": {
                            "issuetype": {
                                "avatarId": 10316,
                                "description": "The sub-task of the issue",
                                "iconUrl": "https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype",
                                "id": "10101",
                                "name": "Sub-task",
                                "self": "https://jira.corp.company.com/rest/api/2/issuetype/10101",
                                "subtask": true
                            },
                            "priority": {
                                "iconUrl": "https://jira.corp.company.com/images/icons/priorities/medium.svg",
                                "id": "3",
                                "name": "Medium",
                                "self": "https://jira.corp.company.com/rest/api/2/priority/3"
                            },
                            "status": {
                                "description": "",
                                "iconUrl": "https://jira.corp.company.com/",
                                "id": "10000",
                                "name": "Backlog",
                                "self": "https://jira.corp.company.com/rest/api/2/status/10000",
                                "statusCategory": {
                                    "colorName": "blue-gray",
                                    "id": 2,
                                    "key": "new",
                                    "name": "To Do",
                                    "self": "https://jira.corp.company.com/rest/api/2/statuscategory/2"
                                }
                            },
                            "summary": "Disable user account in Local AD"
                        },
                        "id": "16916",
                        "key": "TECH-728",
                        "self": "https://jira.corp.company.com/rest/api/2/issue/16916"
                    }
                ],
                "summary": "disable user mm",
                "timeestimate": null,
                "timeoriginalestimate": null,
                "timespent": null,
                "updated": "2018-03-06T21:27:22.000+0000",
                "versions": [],
                "votes": {
                    "hasVoted": false,
                    "self": "https://jira.corp.company.com/rest/api/2/issue/TECH-726/votes",
                    "votes": 0
                },
                "watches": {
                    "isWatching": true,
                    "self": "https://jira.corp.company.com/rest/api/2/issue/TECH-726/watchers",
                    "watchCount": 1
                },
                "workratio": -1
            },
            "id": "16914",
            "key": "TECH-726",
            "self": "https://jira.corp.company.com/rest/api/2/issue/16914"
        }
    ],
    "maxResults": 50,
    "startAt": 0,
    "total": 1
}

如何仅提取子任务部分中的部分

(子任务:true)?

尝试过:

jq -r '.issues[] | .fields.issuetype.subtask' 1.json # and
jq -r '.subtasks[] |.fields.issuetype.subtask' 1.json

但总是出错

jq -r '.subtasks[] | .fields.issuetype.subtask' 1.json
jq: error (at 1.json:231): Cannot iterate over null (null)

最佳答案

jq解决方案:

jq '.issues[].fields.subtasks[].fields | .issuetype.subtask, .summary' 1.json

输出:

true
"Disable user account in Local AD"

关于json - 搜索 JSON 文件 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49140588/

相关文章:

linux - 在 Unix while-do 循环中,比较条件失败

security - 在 bash 脚本执行之间保存密码

json - 人们如何处理 RESTful api 的身份验证(技术不可知)

javascript - 将 JSON 从文本字段解析为对象数组

python - Pandas -Python : How do you write new lines in Pandas?

android - 64 位的 android sdk 是否有它的可执行文件显示一些关于 32 位的信息?

Linux 脚本显示所有登录学生的姓名和他们的模拟号码登录

bash - bash 中的间接变量赋值

json - 如何在客户端生成JSON

java - 将 JSON 字符串转换为 Java/Python (Jython) 对象?