1

Im looking to reformat some json using jq instead of using Python. I have the following json array of objects. I am looking to change this to an array of keys. The values of the keys should be the object.

[{
        "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"
    }
]

The following is the desired output.

{
    "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"
        }
    }]
}

I'd need to grab the value of the repo_name key from each object somehow. Is this possible?

1 Answer 1

2

The following filter produces results in the desired format, but the output marked in the Q as "desired" does not exactly match the given input, which I take to be an oversight.

{repos: map( {(.repo_name): del(.repo_name)} )}
Sign up to request clarification or add additional context in comments.

1 Comment

That's great! I wasn't even close on my path. [to_entries[] | .value.repo_id ] Thank you. Ill select your answer as soon as it lets me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.