0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic, thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.