1

Hi I have a JSON file like followin:

{"a" :[ 
       {
            "b": "name_1",
            "c":[
                   {"d": "value_1", "e": "true"},
                   {"d": "value_2"},
                   {"d": "value_3", "e": "true"}
                ]
       },
       {
           "b": "name_2",
           "c":[
                   {"d": "value_4", "e": "true"},
                   {"d": "value_5"},
                   {"d": "value_6"}
                ]}
       ] 

}

I wanted to delete every object in "c" that contains a key "e".

I tried to do is with JQ but I am not able to get the right commands/filters ...

the nearest I have come ... https://jqplay.org/s/fDzVed_Isv

OcK
  • 93
  • 13

2 Answers2

1

to delete an Object containing the key you have to type:

jq 'del( .a[].c[] | select(has("e")))' 

https://jqplay.org/s/B-JW8wnqG3

Thanks to @steeldriver who answered this question in this post https://askubuntu.com/questions/1046215/find-substring-in-file/1046230?noredirect=1#comment1706714_1046230

OcK
  • 93
  • 13
0

Checking your link, I see you're return NULL for those deletions.

You may want to use splice, instead of delete.

reference: Delete Javascript object item adding null value

Otherwise, you can do this with a map within a map.

var obj = {"a" :[ 
       {
            "b": "name_1",
            "c":[
                   {"d": "value_1", "e": "true"},
                   {"d": "value_2"},
                   {"d": "value_3", "e": "true"}
                ]
       },
       {
           "b": "name_2",
           "c":[
                   {"d": "value_4", "e": "true"},
                   {"d": "value_5"},
                   {"d": "value_6"}
                ]}
       ] 

}
//console.log('obj is: ', obj)
//console.log('obj.a is: ', obj.a)
var array = obj.a
var x = array.map(function(eachThing) {
    var newArr = eachThing.c
  newArr.map(function(eachObj) {
  console.log(eachObj)
  if (eachObj.e === "true")
  console.log('delete this!')
  })
})
x

jsfiddle: http://jsfiddle.net/ethanryan/06mq5uk8/

Ethan Ryan
  • 467
  • 10
  • 16