【发布时间】:2021-08-10 15:30:10
【问题描述】:
受here 解决方案的启发,如果在$paths 中找不到任何JSON 条目,我想从components.schemas 中删除它
这是我的带有jq 的 Powershell 脚本:
$paths = jq '.. |.\"$ref\"? | select(. != null)' $srcJson
jq '.components.schemas |= with_entries( select( .key as $key | any(${paths}[]; index($key) )))' $srcJson | Out-File $destinationJson -Encoding UTF8
这是更改前的示例 JSON 文件:https://codebeautify.org/online-json-editor/cbc78213
然后这是我运行上面的 Powershell 脚本后的预期 JSON 文件:https://codebeautify.org/online-json-editor/cb8c4b2b
请注意,BadSchemas 已从 components.schemas 中删除
当我在 PowerShell 中运行代码时,我收到以下错误消息:
jq: error: syntax error, unexpected '{', expecting IDENT or loc (Windows cmd shell quoting issues?) at , line 1: .components.schemas |= with_entries( select( .key as $key | any(${paths}[]; index($key) )))
谁能告诉我我哪里做错了?
谢谢
【问题讨论】:
-
$ref 是否设置了一些东西?第一行对我来说是空白的。什么是示例 json?我知道双引号需要反斜杠。
-
@js2010 这是带有 jq 命令的 JSON 截图 jqplay.org/s/kNkA2aDWPC
-
哦,物业的名字字面意思是
$ref?这很奇怪。 -
@js2010,
$ref由openapi生成器自动生成
标签: json powershell jq