jq:将对象的json数组输出到带有对象的键数组

时间:2018-11-20 18:19:32

标签: json bash jq

我希望使用jq而不是Python重新格式化某些json。我有以下对象的json数组。我希望将其更改为键数组。键的值应该是对象。

[{
        "repo_expire": "21,600 second(s) (last: Tue Nov 20 07:14:05 2018)",
        "repo_url": "http://repos.blah.com/CentOS/dev/6/os/x86_64/",
        "repo_tstsync": 0,
        "repo_id": "base",
        "repo_pkgs": "6,713",
        "repo_prdsync": 0,
        "repo_revision": 1530286202,
        "repo_name": "CentOS-6 - Base",
        "repo_size": "5.5 G",
        "repo_updated": "Fri Jun 29 08:37:23 2018"
    },
    {
        "repo_expire": "21,600 second(s) (last: Tue Nov 20 07:36:50 2018)",
        "repo_url": "http://blah.com/epel/dev/6/x86_64/",
        "repo_tstsync": 0,
        "repo_id": "epel",
        "repo_pkgs": "12,448",
        "repo_prdsync": 699,
        "repo_revision": 1542329363,
        "repo_name": "Extra Packages for Enterprise Linux 6 - x86_64",
        "repo_size": "11 G",
        "repo_updated": "Thu Nov 15 17:50:18 2018"
    },
    {
        "repo_expire": "21,600 second(s) (last: Tue Nov 20 07:44:05 2018)",
        "repo_url": "http://blah.com/CentOS/dev/6/extras/x86_64/",
        "repo_tstsync": "null",
        "repo_id": "extras",
        "repo_pkgs": 33,
        "repo_prdsync": "null",
        "repo_revision": 1537445728,
        "repo_name": "CentOS-6 - Extras",
        "repo_size": "12 M",
        "repo_updated": "Thu Sep 20 05:15:29 2018"
    }
]

以下是所需的输出。

{
    "repos": [{
        "CentOS-6 - Base": {
            "repo_expire": "21,600 second(s) (last: Tue Nov 20 01:14:05 2018)",
            "repo_url": "http://blah.com/CentOS/dev/6/os/x86_64/",
            "repo_tstsync": 0,
            "repo_id": "base",
            "repo_pkgs": "6,713",
            "repo_prdsync": 0,
            "repo_revision": 1530286202,
            "repo_size": "5.5 G",
            "repo_updated": "Fri Jun 29 08:37:23 2018"
        },
        "Extra Packages for Enterprise Linux 6 - x86_64": {
            "repo_expire": "21,600 second(s) (last: Tue Nov 20 01:36:50 2018)",
            "repo_url": "http://blah.com/epel/dev/6/x86_64/",
            "repo_tstsync": 0,
            "repo_id": "epel",
            "repo_pkgs": "12,448",
            "repo_prdsync": 699,
            "repo_revision": 1542329363,
            "repo_size": "11 G",
            "repo_updated": "Thu Nov 15 17:50:18 2018"
        },
        "CentOS-6 - Extras": {
            "repo_expire": "21,600 second(s) (last: Tue Nov 20 01:44:05 2018)",
            "repo_url": "http://blah.com/CentOS/dev/6/extras/x86_64/",
            "repo_tstsync": "null",
            "repo_id": "extras",
            "repo_pkgs": 33,
            "repo_prdsync": "null",
            "repo_revision": 1537445728,
            "repo_size": "12 M",
            "repo_updated": "Thu Sep 20 05:15:29 2018"
        }
    }]
}

我需要以某种方式从每个对象中获取repo_name键的值。这可能吗?

1 个答案:

答案 0 :(得分:0)

以下过滤器以所需格式生成结果,但是Q中标记为“所需”的输出与给定输入不完全匹配,我认为这是疏忽大意。

 console.log(existingDoc['total']);