Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq: error: number and string cannot be added

I'd like to print the value of members and id from below jq output:

$ cat test_|jq -r '.[] | select(.name=="AAA") | .'
{
  "name": "AAA",
  "members": 10,
  "profiles": 0,
  "templates": 0,
  "ldapGroups": 0,
  "ldapMembers": 0,
  "id": "20"
}

Unfortunately it works for one of each only:

$ cat test_|jq -r '.[] | select(.name=="AAA") | .members'
10

with +" "+ I get error:

$ cat test_|jq -r '.[] | select(.name=="AAA") | .members+" "+.id'
jq: error: number and string cannot be added
like image 275
Chris Avatar asked Oct 24 '17 13:10

Chris


1 Answers

I recommend to use string interpolation. It will automatically cast input to a string if necessary:

jq -r '.[]|select(.name=="AAA")|"\(.members) \(.id)"' file.json
like image 156
hek2mgl Avatar answered Sep 21 '22 20:09

hek2mgl