LPby
LPby

Reputation: 529

jq: exclude object with specified key

Input:

{
    "name":"JSON",
    "good":true,
    "target":"yes"
}
{
    "name":"XML",
    "good":false
}

I would like to exclude object WITHOUT key "target", as follow but NOT has:

jq -r ".| select(has(\"target\"))"

expected output:

{
    "name":"XML",
    "good":false
}

tried this:

jq -r " . | del(select(has(\"target\")))"

but there are two returned objects, one of them NULL

null
{
  "good": false,
  "name": "XML"
}

Upvotes: 6

Views: 6893

Answers (1)

user3899165
user3899165

Reputation:

Select those who do not have target; that way, you do not use del:

jq -r 'select(has("target") | not)'

Upvotes: 11

Related Questions