【发布时间】: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
【问题讨论】: