Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove keys that are present in sibling object and have a certain value

Tags:

json

bash

jq

I have the following input file:

{
    "dic": {
        "a": "",
        "b": "",
        "c": "",
        "d": ""
    },
    "remove": {
        "b": true,
        "c": false,
        "d": true
    }
}

I want to remove with jq all elements of the dictionary dic which are also in the dictionary remove with the value true.

This would be the output:

{
    "dic": {
        "a": "",
        "c": ""
    },
    "remove": {
        "b": true,
        "c": false,
        "d": true
    }
}

I am not sure how to do this. I would first need to clean the remove dic and only get the keys with the value true. Then I would need to somehow only delete these keys from dic.

like image 583
Noim Avatar asked Aug 18 '20 15:08

Noim


1 Answers

You don't need anything other than JQ for that.

[.remove | path(.[] | select(.))] as $p | .dic |= delpaths($p)

Online demo

If there might be other values in remove than true and false, use

select(. == true)

instead of

select(.)
like image 115
oguz ismail Avatar answered Nov 15 '22 06:11

oguz ismail