Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jq – print value only where key value pair exists

I have json file like below. I want to print only the value of displayName when ‘"some_condition"’ doesnt exists or value is null. I cant seem to figure this out.

{
 "entities": [
    {
      "displayName": "host123.bcbsfl.com",
      "properties": {
        "kubernetesLabels": {
          "kubernetes.io/hostname": "host123.bcbsfl.com",
          "some_condition": "true",
          "kubernetes.io/arch": "amd64",
          "kubernetes.io/os": "linux",
          "beta.kubernetes.io/arch": "amd64",
          "node.openshift.io/os_id": "rhcos",
          "beta.kubernetes.io/os": "linux",
          "node-role.kubernetes.io/worker": ""
        }
      }
    },
    {
      "displayName": "host567.abc.com",
      "properties": {
        "kubernetesLabels": {
          "kubernetes.io/hostname": "host567.abc.com",
          "kubernetes.io/arch": "amd64",
          "kubernetes.io/os": "linux",
          "beta.kubernetes.io/arch": "amd64",
          "node.openshift.io/os_id": "rhcos",
          "beta.kubernetes.io/os": "linux",
          "node-role.kubernetes.io/worker": ""
        }
      }
    },
    ]
}

I have tried stuff like this but its not the output I’m looking for since it prints the whole object (sorry if terminology is off a bit. Im still trying to learn this).

jq -r ‘.entities[] |.displayName, select(.properties.kubernetesLabels.some_condition==null) ‘ file.json

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You first have to select the objects you want, then feed the selected result into a new filter which extracts the display name:

.entities[]
| select(.properties.kubernetesLabels.some_condition==null)
| .displayName
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading