Jq extract key of array based on value

Given this JSON:

{
    "{c156e78e-a4ac-422b-bf86-afe12f548dfb}": {
        "name": "after-gluster3.8",
        "date": "2017-04-16 14:31:20",
        "state": "poweron",
        "current": false,
        "parent": ""
    }
    ,
    "{d773d5b7-94d4-4f78-a943-f50e4eb68fe0}": {
        "name": "after-sshd-fixes",
        "date": "2017-04-16 16:58:32",
        "state": "poweroff",
        "current": true,
        "parent": "{c156e78e-a4ac-422b-bf86-afe12f548dfb}"
    }
}

      

I need to extract a key based on the name value i.e. I search "after-sshd-fixes"

and then want "{d773d5b7-94d4-4f78-a943-f50e4eb68fe0}"

as output.

using .[]

throws a key out of an array and I'm a bit overwhelmed by the rest of the jq manual to figure out how to put an array key in a temporary value, do a test and then output a specific array of keys: value

+3


source to share


1 answer


jq :

jq 'to_entries[] | select(.value.name == "after-sshd-fixes").key' jsonfile

      



Output:

"{d773d5b7-94d4-4f78-a943-f50e4eb68fe0}"

      

+3


source







All Articles