删除存在于同级对象中并具有一定值的键 [英] remove keys that are present in sibling object and have a certain value

查看:50
本文介绍了删除存在于同级对象中并具有一定值的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输入文件:

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

我想用jq删除字典dic中所有同样位于值remove中的元素.

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

这将是输出:

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

我不确定该怎么做.我首先需要清理remove dic,仅获取值为true的键.然后,我只需要从dic删除这些键即可.

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.

推荐答案

除了JQ,您不需要其他任何东西.

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

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

在线演示

如果remove中可能有除 true false 以外的其他值,请使用

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

select(. == true)

代替

select(.)

这篇关于删除存在于同级对象中并具有一定值的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆