I have the following JSON:
{
"Category": [
{
"SomeKey_1": {
"Property1": 1,
"Property2": false
}
},
{
"SomeKey_2": {
"Property1": 2,
"Property2": true
}
},
{
"OtherKey_1": {
"Property1": 3,
"Property2": false
}
},
{
"OtherKey_2": {
"Property1": 4,
"Property2": false
}
}
]
}
and I would like to delete from the Category[] array those element whose key starts with or contains "Other". So as the result I would like to have this:
{
"Category": [
{
"SomeKey_1": {
"Property1": 1,
"Property2": false
}
},
{
"SomeKey_2": {
"Property1": 2,
"Property2": true
}
}
]
}
I tried to use the Select command but there I am able to select based on the values not the keys.
CodePudding user response:
If all the objects in your array only contain a single property:
.Category |= map(select(keys_unsorted[0] | contains("Other") | not))
If you want to delete objects of which any property's key starts with "Other":
.Category |= map(select(any(keys_unsorted[]; contains("Other")) | not))
And finally, if you only want to delete those objects with only "Other" properties:
.Category |= map(select(all(keys_unsorted[]; contains("Other")) | not))
It's also possible to use the del
filter:
del(.Category[] | select(keys_unsorted[0] | contains("Other")))
del(.Category[] | select(any(keys_unsorted[]; contains("Other"))))
del(.Category[] | select(all(keys_unsorted[]; contains("Other"))))