简体   繁体   中英

jq parse for multiple values

I'm trying to output displayName from JSON which has both

"source": "0.0.0.0/0" and
tcpOptions": "destinationPortRange": "min": 80

The result should display only

rule-1

eg: JSON

[
  {
    "displayName": "rule-1",
    "secrule": [
      {
        "source": "0.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 80,
            "max": 80
          }
        }
      },
      {
        "source": "0.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 443,
            "max": 443
          }
        }
      }
    ]
  },
  {
    "displayName": "rule-2",
    "secrule": [
      {
        "source": "0.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 443,
            "max": 443
          }
        }
      },
      {
        "source": "20.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 80,
            "max": 80
          }
        }
      }
    ]
  }
]

I have tried

jq -r '.[] | select(.secrule[].source == "0.0.0.0/0" and .secrule[].tcpOptions.destinationPortRange.min == 80) | .displayName' JSON | sort -u

But it displays both rules (which is incorrect)

rule-1
rule-2

You're expanding .secrule twice, thus every combination of its elements get checked. Use any instead:

.[] | select(any(.secrule[]; .source=="0.0.0.0/0" and .tcpOptions.destinationPortRange.min==80)).displayName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM