Please note that this post is tagged jq, I am not trying to do this with javascript at all.
I have an object that has an array as one of its fields. I'd like to remove the elements from the object's array that match a criteria, but keep the object around. I've looked around and I've only been able to spot snippets of returning just the array that happens to have less items now.
Here's an example:
{
"foo": "bar",
"events": [
{
"id": 1,
"msg": "starting"
},
{
"id": 2,
"msg": "working on it"
},
{
"id": null,
"msg": "done"
}
]
}
and I'd like to get back the following:
{
"foo": "bar",
"events": [
{
"id": 1,
"msg": "starting"
},
{
"id": 2,
"msg": "working on it"
}
]
}
Note: Just in case this will affect the answer I'm really only interested in the last element. I plan to build up a new object in a following pipe stage like:
jq '$answer | {bar:.foo, event: .events[-1].msg}`
I have tried both
select(.events[].id != null)
and
del(.events[], select(.id == null)))
with no luck.
jqsyntax.jqthe json query tool, not jQuery.