I get a long list of such arrays

    "id": "byu6g6c4cjys5mdkg5znh8ju8c",
    "create_at": 1511875272294,
    "update_at": 1511875272294,
    "delete_at": 0,
    "display_name": "BMA",
    "name": "BMA",
    "description": "",
    "email": "aleksandar.ivanov@random.com",
    "type": "O",
    "company_name": "",
    "allowed_domains": "",
    "invite_id": "gdgz1tbxuinntx1fbr1ax7kehy",
    "allow_open_invite": false,
    "scheme_id": null

I want to get by JQ only the ID where the name is BMA. At the moment I parse " jq -r '.[]["name"]" and I can filter the output from curl by name and I will get "BMA" and also 100 other names, but I need to filter only the ID where name is =BMA. Any ideas?



You should be able to accomplish this with the following:

jq '.[] | select (.name=="BMA") | .id'

If name is BMA it will print id


json -c 'this.name === "BMA"' -a id
  • Hello, I manage do it now , but I get this output though: jq: error: deposit/0 is not defined at <top-level>, line 1: .[] | select (.name==deposit-solutions) | .id jq: error: solutions/0 is not defined at <top-level>, line 1: .[] | select (.name==deposit-solutions) | .id jq: 2 compile errors Any ideas? – WhoAmI Nov 29 '18 at 18:23
  • @DenisHristov: Make sure you quote the names you are searching for as they are not defined variables. Should be select (.name=="deposit-solutions") for example. – Jesse_b Nov 29 '18 at 18:25
  • I cannot quote the names output as it is from another API call and when I do "$test" it doesn't recognize the variable. Any help here? – WhoAmI Nov 29 '18 at 18:30
  • You can try: jq ".[] | select (.name==\"$test\") | .id" – Jesse_b Nov 29 '18 at 18:34
  • This helped - man you are awesome. Last question is : /usr/bin/curl -s -H 'Authorization: Bearer xxxxxx' -X GET -d 'per_page=500' 'blabla.random.com/api/v4/teams/$dam/members/$exist' How to parse the variables. Cause at the moment it is not counting the variables – WhoAmI Nov 29 '18 at 19:00

alternative to jq solution is jtc:

bash $ jtc -w'[name]:<BMA>+0 [-1] [id]' input.json
bash $ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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