如何在 jq 脚本文件的命令中使用环境变量?

发布于 2025-01-17 20:00:39 字数 2095 浏览 0 评论 0原文

我正在处理大量 OpenAPI 3.0.0 文件,我需要从中创建 html 页面。每个 OpenAPI 文件代表大对象模型中的一个实体。 Swagger Viewer、redoc-cli 和其他工具无法生成我需要的文档类型。这是一个示例文件:

{
    "openapi": "3.0.0",
    "paths": {},
    "info": {
        "title": "My File",
        "version": "1.0.0"
    },
    "components": {
        "schemas": {
            "my_entity_name": {
                "type": "object",
                "description": "....",
                "properties": {
                    "propertyOne": {
                        "type": "string",
                        "description": "..."
                    },
                    "propertyTwo": {
                        "type": "string",
                        "format": "date",
                        "description": "..."
                    }
                }
            }
        }
    }
}

在我的解决方案中,我使用 jq 来解析文件(macOS、ksh)。由于有这么多,我想将一个环境变量传递给jq。这在 shell 中很简单:

 % entity_name=my_entity_name
 % jq -r 'select(.components.schemas.'$entity_name'.properties != null)' file.json

在脚本文件中,方法是类似的,并且我得到了我期望的结果。但是,考虑到我想要做的事情的性质,我想使用 jq -f 选项将 jq 命令放入文件中。

我尝试将以下内容放入名为 jqscript.jq 的文件中:

 select(.components.schemas.'$entity_name'.properties != null)

并且我这样调用:

  % jq -rf jqscript.jq --arg entity_name "$entity_name" file.json

这会导致错误:

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
select(.components.schemas.'$entity_name'.properties != null)                           
jq: 1 compile error

我试图破译如何在此类中使用环境变量将 '$entity_name' 修改为 $entity_name(删除单引号)、$[entity_name]${entity_name} 等,但我得到了类似的结果。

我能够使用以下示例脚本验证变量是否可以传递到 jq 脚本文件:

value: $entity_name
} |
.value

当我使用 % jq -rf jqscript.jq --arg entity_name "$entity_name" file.json 运行此文件时code>,我得到了返回的entity_name的预期值。

如何使用 jq 命令中和 jq 脚本文件中的命令的环境变量?

I am working with a large quantity of OpenAPI 3.0.0 files from which I need to create html pages. Each OpenAPI file represents an entity in a large object model. The Swagger Viewer, redoc-cli, and other tools do not produce the type of documentation I need. Here is an example file:

{
    "openapi": "3.0.0",
    "paths": {},
    "info": {
        "title": "My File",
        "version": "1.0.0"
    },
    "components": {
        "schemas": {
            "my_entity_name": {
                "type": "object",
                "description": "....",
                "properties": {
                    "propertyOne": {
                        "type": "string",
                        "description": "..."
                    },
                    "propertyTwo": {
                        "type": "string",
                        "format": "date",
                        "description": "..."
                    }
                }
            }
        }
    }
}

Within my solution I am using jq to parse the files (macOS, ksh). Since there are so many, I would like to pass an environment variable to jq. This is straightforward in the shell:

 % entity_name=my_entity_name
 % jq -r 'select(.components.schemas.'$entity_name'.properties != null)' file.json

In a script file the approach is similar, and I get the results I expect. However, given the nature of what I am trying to do, I'd like to use the jq -f option to put the jq commands in a file.

I've attempted putting the following in a file called jqscript.jq:

 select(.components.schemas.'$entity_name'.properties != null)

And I call make the call thus:

  % jq -rf jqscript.jq --arg entity_name "$entity_name" file.json

This results in an error:

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
select(.components.schemas.'$entity_name'.properties != null)                           
jq: 1 compile error

I've tried to decipher how to use an environment variable in such a situation by modifying the '$entity_name' to $entity_name (removing the single quotes),$[entity_name], ${entity_name}, et cetera but I had similar results.

I was able to verify that variables can be passed to jq script files with the following example script:

value: $entity_name
} |
.value

When I run with this file with % jq -rf jqscript.jq --arg entity_name "$entity_name" file.json, I get the expected value for entity_name returned.

How can I go about using an environment variable that is both within a jq command and that command is in a jq script file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-01-24 20:00:39

您正在使用变量的 value 动态构建过滤器。这是脆弱的,因为所得的表达是解析的,而不是使用变量的值(类似于SQL注入攻击)。

您可以使用$ env直接访问环境。

 % export entity_name=my_entity_name
 % jq -r 'select(.components.schemas[$ENV.entity_name].properties != null)' file.json

或简单地将密钥作为明确的参数传递

% jq -r --arg entity_name my_entity_name 'select(.components.schemas[$entity_name].properties != null)' file.json

You are dynamically constructing a filter using the value of a variable. This is fragile, as the resulting expression is parsed, rather than using the value of the variable literally (similar to SQL injection attacks).

You can access the environment directly using $ENV.

 % export entity_name=my_entity_name
 % jq -r 'select(.components.schemas[$ENV.entity_name].properties != null)' file.json

or simply pass the key as an explicit argument

% jq -r --arg entity_name my_entity_name 'select(.components.schemas[$entity_name].properties != null)' file.json
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文