retroman
retroman

Reputation: 195

Remove multiple entries from an array of objects using jq

I have the following json and want to remove multiple entries from the ebooks array if they are not in the following array ["Pascal", "Python"] (will eventually be dynamic array, this is just for example)

{
   "eBooks":[
      {

         "language":"Pascal",
         "edition":"third"
      },
      {
         "language":"Python",
         "edition":"four"
      },
      {
         "language":"SQL",
         "edition":"second"
      }
   ]
}

was hoping to do something like this, which if it worked would delete last one containing the SQL because it's not in the array, but this doesn't work

jq '.ebooks[] | select ( .language | in(["Pascal", "Python"]))' ebooks.json

Upvotes: 0

Views: 458

Answers (1)

pmf
pmf

Reputation: 36261

You're almost there. Use del, IN and a capital B in eBooks :)

jq 'del(.eBooks[] | select(.language | IN("Pascal", "Python")))' ebooks.json
{
  "eBooks": [
    {
      "language": "SQL",
      "edition": "second"
    }
  ]
}

Demo

Upvotes: 2

Related Questions