5

Given the following JSON (oversimplified for the sake of the example), I need to order the keys by their value. In this case, the order should be id > name > type.

{
    "link": [{
            "attributes": [{
                    "value": "ConfigurationElement",
                    "name": "type"
                }, {
                    "value": "NAME1",
                    "name": "name"
                }, {
                    "value": "0026a8b4-ced6-410e-9213-e3fcb28b3aab",
                    "name": "id"
                }
            ],
            "href": "href1",
            "rel": "down"
        }, {
            "attributes": [{
                    "value": "0026a8b4-ced6-410e-9213-k23g15h2u1l5",
                    "name": "id"
                }, {
                    "value": "ConfigurationElement",
                    "name": "type"
                }, {
                    "value": "NAME2",
                    "name": "name"
                }
            ],
            "href": "href2",
            "rel": "down"
        }
    ],
    "total": 2
}

EXPECTED RESULT:

{
    "link": [{
            "attributes": [{
                    "value": "0026a8b4-ced6-410e-9213-e3fcb28b3aab",
                    "name": "id"
                }, {
                    "value": "NAME1",
                    "name": "name"
                }, {
                    "value": "ConfigurationElement",
                    "name": "type"
                }
            ],
            "href": "href1",
            "rel": "down"
        }, {
            "attributes": [{
                    "value": "0026a8b4-ced6-410e-9213-k23g15h2u1l5",
                    "name": "id"
                }, {
                    "value": "NAME2",
                    "name": "name"
                }, {
                    "value": "ConfigurationElement",
                    "name": "type"
                }
            ],
            "href": "href2",
            "rel": "down"
        }
    ],
    "total": 2
}

I would be very grateful if anyone could help me out. I tried jq with -S and -s with sort_by(), but this example is way too complex for me to figure it out with my current experience with jq. Thank you a lot!

1 Answer 1

10

You can do:

jq '.link[].attributes|=sort_by(.name)'

The |= takes all the paths matched by .link[].attributes, i.e. each "attributes" array, and applies the filter sort_by(.name) to each of them, leaving everything else unchanged.

2
  • 2
    jq is such an amazing & powerful thing... I would never have thought of that! Sep 4, 2020 at 11:14
  • incredible.. i was starting to loose my hope after all the tries while the answer was so short and clean. Thank you a lot!!!! Cheers!
    – Future
    Sep 4, 2020 at 11:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.