3

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": "[email protected]",
    "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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
WhoAmI
  • 159
  • 1
  • 4
  • 8

3 Answers3

7

jq

You should be able to accomplish this with the following:

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

If name is BMA it will extract and output the corresponding .id.

To use the value of a shell variable, import it into jq with --arg:

myvariable=BMA
jq --arg name "$myvariable" '.[] | select( .name == $name ).id'

json

json -c 'this.name === "BMA"' -a id
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
jesse_b
  • 35,934
  • 12
  • 91
  • 140
  • Hello, I manage do it now , but I get this output though: jq: error: deposit/0 is not defined at , line 1: .[] | select (.name==deposit-solutions) | .id jq: error: solutions/0 is not defined at , 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' 'https://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
  • Shell variables won't expand inside single quotes (`'`) use double quotes instead (`"`). – jesse_b Nov 29 '18 at 19:04
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/86402/discussion-between-denis-hristov-and-jesse-b). – WhoAmI Nov 29 '18 at 19:14
  • Thanks so much I have no words to explain u how much u helped me – WhoAmI Nov 30 '18 at 07:51
0

alternative to jq solution is jtc:

bash $ jtc -w'[name]:<BMA>+0 [-1] [id]' input.json
"byu6g6c4cjys5mdkg5znh8ju8c"
bash $ 
Dmitry L.
  • 41
  • 2
0

Using Miller (mlr), first for extracting the relevant entries from the larger array (the ones having name equal to BMA), and then for extracting the id field from the selected entries:

mlr --j2n filter '$name == "BMA"' then cut -f id file

The --j2n option tells mlr invocation to read JSON and produce "indexed/tool-kit" output (essentially, just the data).

Given a file with a JSON array containing the object in the question text, this would output the single string byu6g6c4cjys5mdkg5znh8ju8c on a line.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936