【问题标题】:How can I remove a nested key-value pair, whose value has a deeper nested array with a specific value by using jq如何使用 jq 删除嵌套键值对,其值具有更深的嵌套数组和特定值
【发布时间】:2022-01-12 00:44:30
【问题描述】:

我目前正在尝试使用 jq 从 swagger json 文档中过滤掉一些路径值。

JSON 看起来像这样:

{
  "swagger": "2.0",
  "info": {
    "version": "1.0.2",
    "title": "Some API"
  },
  "host": "example.com",
  "basePath": "/",
  "paths": {
    "/api/companies": {
      "get": {
        "tags": [ "company-tag" ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": { "$ref": "#/definitions/CompanyDTO" }
          }
        }
      }
    },
    "/api/account": {
      "get": {
        "tags": [ "account-tag" ],
        "operationId": "getAccount",
        "responses": {
          "200": {
            "description": "OK",
            "schema": { "$ref": "#/definitions/UserDTO" }
          }
        }
      },
      "post": {
        "tags": [ "account-tag" ],
        "operationId": "createAccount",
        "responses": {
          "200": {
            "description": "OK",
            "schema": { "$ref": "#/definitions/UserDTO" }
          }
        }
      }
    }
  }
}

我想过滤 paths 值,以便仅获取 tags 数组中包含特定标签的路径。

例如:如果我想获取 account-tag 的路径,过滤后的 JSON 应该如下所示:

{
  "swagger": "2.0",
  "info": {
    "version": "1.0.2",
    "title": "Some API"
  },
  "host": "example.com",
  "basePath": "/",
  "paths": {
    "/api/account": {
      "get": {
        "tags": [ "account-tag" ],
        "operationId": "getAccount",
        "responses": {
          "200": {
            "description": "OK",
            "schema": { "$ref": "#/definitions/UserDTO" }
          }
        }
      },
      "post": {
        "tags": [ "account-tag" ],
        "operationId": "createAccount",
        "responses": {
          "200": {
            "description": "OK",
            "schema": { "$ref": "#/definitions/UserDTO" }
          }
        }
      }
    }
  }
}

编辑

池上的第二个建议如预期般奏效。 这是我最后的solution

【问题讨论】:

    标签: json jq


    【解决方案1】:

    如果我们假设给定路径的所有方法(getpost 等)共享相同的标签,我们可以简单地使用以下代码:

    .paths |= with_entries( select( .value[].tags | index("account-tag") ) )
    

    Demojqplay


    如果该假设不成立,我们将需要在两个级别进行过滤。

    .paths |= with_entries(
       .value |= with_entries(
          select( .value.tags | index("account-tag") )
       ) |
       select( .value | length > 0 )
    )
    

    Demojqplay

    .paths |= with_entries(
       .value = (
          .value |
          with_entries(
             select( .value.tags | index("account-tag") )
          ) |
          select( length > 0 )
       )
    )
    

    Demojqplay

    (我讨厌我必须在这里使用.value = ( .value | ... ) 而不是.value |= ( ... )。)

    【讨论】:

    • 第一个假设不能成立。但是,您的第二个建议似乎按预期工作。我想将您的答案标记为正确:) 您可以将此小演示添加到您的答案中吗? jqplay.org/s/4VmYatAx-- 它包含完整的查询以及问题中的示例
    • 还检查了我在本地放置的更大的 json,它似乎按预期工作。非常感谢:)
    • 我故意不使用 OP 中的 JSON,因为它没有显示代码有效。它没有 getpost 具有不同标签的 URL。
    • 是的,在讨论过程中已经删除的答案中,json 有点进化了,所以在你修补问题时,它被编辑了。对于那个很抱歉。我还意识到将演示添加到问题中就足够了,所以不用担心。再次感谢它为我节省了很多时间:D
    猜你喜欢
    • 2021-04-22
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多