使用bash搜索JSON文件

时间:2018-02-22 21:11:05

标签: json bash jq

我有JSON文件,并且只想提取一些值

如何仅提取"键":"TECH-456", 和"键":"TECH",(键后的值)

{
    "expand": "names,schema",
    "issues": [
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "fields": {
                "aggregateprogress": {
                    "percent": 100,
                    "progress": 86400,
                    "total": 86400
                },
                "aggregatetimeestimate": 0,
                "aggregatetimeoriginalestimate": 57600,
                "aggregatetimespent": 86400,
                "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": "user1",
                    "emailAddress": "user1@gmail.com",
                    "key": "user1",
                    "name": "user1",
                    "self": "https://mycompany/rest/api/2/user?username=user1",
                    "timeZone": "Europe/Belgrade"
                },
                "components": [],
                "created": "2018-01-10T10:23:01.000+0000",
                "creator": {
                    "active": true,
                    "avatarUrls": {
                        "16x16": "https://mycompany/secure/useravatar?size=xsmall&avatarId=10349",
                        "24x24": "https://mycompany/secure/useravatar?size=small&avatarId=10349",
                        "32x32": "https://mycompany/secure/useravatar?size=medium&avatarId=10349",
                        "48x48": "https://mycompany/secure/useravatar?avatarId=10349"
                    },
                    "displayName": "user2",
                    "emailAddress": "user2@gmail.com",
                    "key": "user2",
                    "name": "user2",
                    "self": "https://mycompany/rest/api/2/user?username=user2",
                    "timeZone": "Zulu"
                },
                "customfield_10000": null,
                "customfield_10001": null,
                "customfield_10002": null,
                "customfield_10004": "0|i00n0f:",
                "customfield_10005": null,
                "customfield_10006": null,
                "customfield_10100": null,
                "customfield_10101": [],
                "customfield_10102": null,
                "customfield_10103": null,
                "customfield_10107": {
                    "id": "10400",
                    "self": "https://mycompany/rest/api/2/customFieldOption/10400",
                    "value": "Hentsu Internal"
                },
                "customfield_10108": null,
                "customfield_10200": null,
                "customfield_10201": "2018-01-12",
                "customfield_10202": "2018-01-12",
                "customfield_10203": null,
                "customfield_10204": null,
                "customfield_10205": null,
                "customfield_10206": null,
                "customfield_10300": "com.atlassian.servicedesk.plugins.approvals.internal.customfield.ApprovalsCFValue@40efdb56",
                "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": ".",
                "duedate": null,
                "environment": null,
                "fixVersions": [],
                "issuelinks": [],
                "issuetype": {
                    "avatarId": 10318,
                    "description": "A task that needs to be done.",
                    "iconUrl": "https://mycompany/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
                    "id": "10100",
                    "name": "Task",
                    "self": "https://mycompany/rest/api/2/issuetype/10100",
                    "subtask": false
                },
                "labels": [],
                "lastViewed": "2018-02-22T15:47:47.792+0000",
                "priority": {
                    "iconUrl": "https://mycompany/images/icons/priorities/medium.svg",
                    "id": "3",
                    "name": "Medium",
                    "self": "https://mycompany/rest/api/2/priority/3"
                },
                "progress": {
                    "percent": 100,
                    "progress": 86400,
                    "total": 86400
                },
                "project": {
                    "avatarUrls": {
                        "16x16": "https://mycompany/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201",
                        "24x24": "https://mycompany/secure/projectavatar?size=small&pid=10001&avatarId=10201",
                        "32x32": "https://mycompany/secure/projectavatar?size=medium&pid=10001&avatarId=10201",
                        "48x48": "https://mycompany/secure/projectavatar?pid=10001&avatarId=10201"
                    },
                    "id": "10001",
                    "key": "TECH",
                    "name": "Technology",
                    "self": "https://mycompany/rest/api/2/project/10001"
                },
                "reporter": {
                    "active": true,
                    "avatarUrls": {
                        "16x16": "https://mycompany/secure/useravatar?size=xsmall&avatarId=10349",
                        "24x24": "https://mycompany/secure/useravatar?size=small&avatarId=10349",
                        "32x32": "https://mycompany/secure/useravatar?size=medium&avatarId=10349",
                        "48x48": "https://mycompany/secure/useravatar?avatarId=10349"
                    },
                    "displayName": "user2",
                    "emailAddress": "user2@gmail.com",
                    "key": "user2",
                    "name": "user2",
                    "self": "https://mycompany/rest/api/2/user?username=user2",
                    "timeZone": "Zulu"
                },
                "resolution": null,
                "resolutiondate": null,
                "status": {
                    "description": "",
                    "iconUrl": "https://mycompany/images/icons/statuses/inprogress.png",
                    "id": "3",
                    "name": "In-Progress",
                    "self": "https://mycompany/rest/api/2/status/3",
                    "statusCategory": {
                        "colorName": "yellow",
                        "id": 4,
                        "key": "indeterminate",
                        "name": "In Progress",
                        "self": "https://mycompany/rest/api/2/statuscategory/4"
                    }
                },
                "subtasks": [],
                "summary": "Build the Fortimanage Portal",
                "timeestimate": 0,
                "timeoriginalestimate": 57600,
                "timespent": 86400,
                "updated": "2018-01-31T15:22:06.000+0000",
                "versions": [],
                "votes": {
                    "hasVoted": false,
                    "self": "https://mycompany/rest/api/2/issue/TECH-456/votes",
                    "votes": 0
                },
                "watches": {
                    "isWatching": false,
                    "self": "https://mycompany/rest/api/2/issue/TECH-456/watchers",
                    "watchCount": 1
                },
                "workratio": 150
            },
            "id": "15377",
            "key": "TECH-456",
            "self": "https://mycompany/rest/api/2/issue/15377"
        }
    ],
    "maxResults": 50,
    "startAt": 0,
    "total": 1
}

1 个答案:

答案 0 :(得分:1)

使用 jq 工具的正确方法:

jq -r '.issues[0] | .key, .fields.project.key' file.json

输出:

TECH-456
TECH