Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete an element when a key of element is not found by jq

Tags:

json

select

jq

I want to delete some elements when a specific key is not found using jq. In below json file, I need to delete the first and second items as they don't have the key "Case".

[
  {
    "label": "US : USA : English",
    "Country": "USA",
    "region": "US",
    "Language": "English",
    "locale": "en",
    "currency": "USD",
    "number": "USD"
  },
  {
    "label": "AU : Australia : English",
    "Country": "Australia",
    "region": "AU",
    "Language": "English",
    "locale": "en",
    "currency": "AUD",
    "number": "AUD"
  },
  {
    "Case": "1",
    "label": "CA : Canada : English",
    "Country": "Canada",
    "region": "CA",
    "Language": "English",
    "locale": "en",
    "currency": "CAD",
    "number": "CAD"
  }
]

And the output would be:

[
  {
    "Case": "1",
    "label": "CA : Canada : English",
    "Country": "Canada",
    "region": "CA",
    "Language": "English",
    "locale": "en",
    "currency": "CAD",
    "number": "CAD"
  }
]
like image 212
Jason Avatar asked Oct 28 '25 07:10

Jason


1 Answers

Just figured out my solution:

del(.[] | select(.Case == null))
like image 150
Jason Avatar answered Oct 30 '25 23:10

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!