简体   繁体   中英

jq and bash : get a specific value in json when other key value pair matches

I want to parse a JSON file to get a specific value where the condition is another key/value pair, but I don't know how to handle this (I am not a developer). For example, I get this JSON

[
    {
        "id": "c490ffaf-92ea-4a29-9614-ab36a088f366",
        "clientId": "account",
        "name": "${client_account}",
        "rootUrl": "${authBaseUrl}",
        "baseUrl": "/realms/test1/account/",
        "surrogateAuthRequired": false,
        "enabled": true,
        "clientAuthenticatorType": "client-secret",
    },
    {
        "id": "2daa32334-24322-4a29-43242-423223",
        "clientId": "swap",
        "name": "${client_swap}",
        "rootUrl": "${authBaseUrl}",
        "baseUrl": "/realms/test/swap/",
        "surrogateAuthRequired": false,
        "enabled": true,
        "clientAuthenticatorType": "client-secret",
    },
    {
        "id": "e22112-11111-4a329-22211-333312",
        "clientId": "broker",
        "name": "${client_broker}",
        "rootUrl": "${authBaseUrl}",
        "baseUrl": "/realms/test/broker/",
        "surrogateAuthRequired": false,
        "enabled": true,
        "clientAuthenticatorType": "client-secret",
    },
]

I want the value of "id" (c490ffaf-92ea-4a29-9614-ab36a088f366 in this example) from the block where the value from "clientId" is "account"

How to do that kind of thing with jq and bash?

In case there's more than one occurrence, it might be wise to use the following query:

.[]
| select(.clientId == "account")
| .id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM