如果在PowerShell中使用JQ在其他键中找不到该值,如何删除整个JSON条目? [英] How to delete entire JSON entry if the value is not found in other keys using jq in Powershell?

查看:26
本文介绍了如果在PowerShell中使用JQ在其他键中找不到该值,如何删除整个JSON条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
    "openapi": "3.0.1",
    "paths": {
        "/api/car": {
            "summary": "Something about Duration"
        },
        "/api/plane": {
            "summary": "Something about Duration"
        }
    },
    "components": {
        "schemas": {
            "Duration": {
                "enum": [
                    "OneMonth",
                    "ThreeMonth"
                ]
            },
            "Apple": {
                "type": "object",
                "properties": {
                    "flaggedDataCount": {
                        "type": "integer",
                        "description": "someting",
                        "format": "int32"
                    }
                }
            }
        }
    }
}

我上面有一个JSON条目。 我想要递归删除schemas中未在paths值中找到的任何JSON条目。

例如,Durationpaths处的值中找到,但Apple的值中找不到。

因此Apple条目应从JSON文件中删除。

如何使用Powershell中的jq执行此操作?

我想过下面的代码,但我不知道:

  • 从组件漫游
  • 统计JSON文件其余部分中test
  • 的密钥值

walk(if type == "object????" then with_entries(select(.key | test("?????") | not)) else . end)

推荐答案

您说得对,with_entries(select(...))成语的相关性:

[.paths[].summary] as $texts
| .components.schemas |=
    with_entries( select( .key as $key | any($texts[]; index($key) )))

这篇关于如果在PowerShell中使用JQ在其他键中找不到该值,如何删除整个JSON条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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