Search code examples
arraysif-statementsyntaxjqboolean-operations

Use or operator in jq


I'm using jq to parse JSON. The JSON has an array called "players[]" which contains a key "name" with the following sample values:

ZZZ-104TH-082-ADV-004
ZZZ-149TH-435-ADV-WL-006
GDS-123-CIC-007_temp-blocked
LRMR-121-CIC-002_temp-removed
NOS-343-CIC-003_Failed
E180TH-426-CIC-008
LBRTY-185-CIC-005

I have this jq line:

jq '.players[] | if .name | contains( "ZZZ" ) then empty else .name end'

Which returns the following names:

GDS-123-CIC-007_temp-blocked
LRMR-121-CIC-002_temp-removed
NOS-343-CIC-003_Failed
E180TH-426-CIC-008
LBRTY-185-CIC-005

I would like to also exclude names that contain "Failed" or "temp" which would return the following:

E180TH-426-CIC-008
LBRTY-185-CIC-005

I tried

jq '.players[] | if .name | contains( "ZZZ" ) or contains( "Failed" ) then empty else .name end'

But that only excluded the names containing "ZZZ". how can I filter out names that contain "ZZZ" or "Failed" or "temp"?


Solution

  • I'd go with select (which emits empty if its argument evaluates to false, i.e. there is no match), and test for regular expressions:

    jq '.players[].name | select(test("ZZZ|temp|Failed") | not) '
    
    "E180TH-426-CIC-008"
    "LBRTY-185-CIC-005"