jq select objects based on a value

  • Initial json input
{
  "ACTION": "domain.list",
  "DATA": [
    {
      "TYPE": "slave",
      "AXFR_IPS": "none",
      "DOMAIN": "domain1.com",
      "TTL_SEC": 0,
      "SOA_EMAIL": "",
      "DOMAINID": 11111,
      "DESCRIPTION": "",
      "EXPIRE_SEC": 0,
      "RETRY_SEC": 0,
      "STATUS": 1,
      "LPM_DISPLAYGROUP": "",
      "REFRESH_SEC": 0
    },
    {
      "TYPE": "slave",
      "AXFR_IPS": "none",
      "DOMAIN": "domain2",
      "TTL_SEC": 0,
      "SOA_EMAIL": "",
      "DOMAINID": 22222,
      "DESCRIPTION": "",
      "EXPIRE_SEC": 0,
      "RETRY_SEC": 0,
      "STATUS": 1,
      "LPM_DISPLAYGROUP": "",
      "REFRESH_SEC": 0
    },
    {
      "TYPE": "slave",
      "AXFR_IPS": "none",
      "DOMAIN": "domain3",
      "TTL_SEC": 0,
      "SOA_EMAIL": "",
      "DOMAINID": 33333,
      "DESCRIPTION": "",
      "EXPIRE_SEC": 0,
      "RETRY_SEC": 0,
      "STATUS": 1,
      "LPM_DISPLAYGROUP": "",
      "REFRESH_SEC": 0
    }
  ],
  "ERRORARRAY": []
}
  • Return the DOMAINID from domain2
jq '.DATA[]  | select(.DOMAIN == "domain2") | .DOMAINID'
22222
  • Return DOMAINID and TYPE from domain2
jq '.DATA[]  | select(.DOMAIN == "domain2") | .DOMAINID,.TYPE'
22222
"slave"
  • Return DOMAINID, DOMAIN from domain2 and domain3
jq '.DATA[]  | select(.DOMAIN == ("domain2","domain3")) | .DOMAINID,.DOMAIN'
22222
"slave"
33333
"slave"

# or

jq '.DATA[]  | select(.DOMAIN | IN("domain2", "domain3")) | .DOMAINID,.DOMAIN'
22222
"slave"
33333
"slave"

References:

  • 1 Github Issue

  1. https://github.com/stedolan/jq/issues/370 ↩︎

Previous
Next