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 del: Remove elements from a list

I want delete sig JSON list entries that have the enabled key with the value false.

The following jq cmd does select the false entries but now I want remove those entries and keep the rest of the structure.

jq '.app.sig[] | select(.enabled==false)'  app-test.json

Example JSON input

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

{
  "app" : {
       "id" : "11111111",
       "name" : "test",
       "general" : {
           "allow" : "sdasdass",
       },
       "settings" : {
           "max" : "8192",
       },
       "sig" : [
           {
               "enabled" : true,
               "Staging" : false,
               "sId" : "200101556"
           },
           {
               "enabled" : false,
               "Staging" : false,
               "sId" : "200012071"
           },
           {
               "enabled" : true,
               "Staging" : false,
               "sId" : "200012002"
           },
       ],
       "version" : "v1",
   }
}

Any help appreciated.

Thanks

>Solution :

WIth a valid JSON input it’s straightforward using del and select:

del(.app.sig[] | select(.enabled | not))

Demo

Alternatively, use map to reset the array with what you want to keep:

.app.sig |= map(select(.enabled))

Demo

In both cases, you can directly use the (possibly negated) boolean value withing select. Output:

{
  "app": {
    "id": "11111111",
    "name": "test",
    "general": {
      "allow": "sdasdass"
    },
    "settings": {
      "max": "8192"
    },
    "sig": [
      {
        "enabled": true,
        "Staging": false,
        "sId": "200101556"
      },
      {
        "enabled": true,
        "Staging": false,
        "sId": "200012002"
      }
    ],
    "version": "v1"
  }
}
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