简体   繁体   English

使用 jq 创建 JSON 文件

[英]Create JSON file using jq

I'm trying to create a JSON file by executing the following command:我正在尝试通过执行以下命令来创建一个 JSON 文件:

jq --arg greeting world '{"hello":"$greeting"}' > file.json

This command stuck without any input.这个命令没有任何输入就卡住了。 While虽然

jq -n --arg greeting world '{"hello":"$greeting"}' > file.json doesn't parse correctly. jq -n --arg greeting world '{"hello":"$greeting"}' > file.json解析不正确。 I'm just wondering is really possible to create a JSON file.我只是想知道是否真的可以创建一个 JSON 文件。

So your code doesn't work because included the variable inside double quotes which gets treated as string.所以你的代码不起作用,因为在双引号内包含了被视为字符串的变量。 That is why it is not working这就是为什么它不起作用

As @Jeff Mercado, pointed out the solution is正如@Jeff Mercado,指出解决方案是

jq -n --arg greeting world '{"hello":$greeting}' > file.json

About the - in a name.关于-在一个名字。 This is actually possible.这实际上是可能的。 But as of now this is not available in released version of jq.但截至目前,这在 jq 的发布版本中不可用。 If you compile the master branch of jq on your system.如果你在你的系统上编译 jq 的 master 分支。 There is a new variable called $ARGS.named which can be used to access the information.有一个名为$ARGS.named的新变量可用于访问信息。

I just compiled and check the below command and it works like a charm我刚刚编译并检查了下面的命令,它就像一个魅力

./jq -n --arg name-is tarun '{"name": $ARGS.named["name-is"]}'
{
  "name": "tarun"
}

To add to what Jeff and Tarun have already said, you might want to use the \\() string interpolation syntax in your command.为了补充JeffTarun已经说过的内容,您可能希望在命令中使用\\()字符串插值语法。 eg.例如。

jq -n --arg greeting world '{"hello":"\($greeting)"}'

for me this produces对我来说这会产生

{
  "hello": "world"
}

Regarding your reply to Jeff's comment, the argument name you choose has to be a valid jq variable name so an arg like greeting-for-you won't work but you could use underscores so greeting_for_you would be ok.关于您对杰夫评论的回复,您选择的参数名称必须是有效的 jq 变量名称,因此像greeting-for-you这样的参数将不起作用,但您可以使用下划线,这样greeting_for_you就可以了。 Or you could use the version Tarun described.或者您可以使用 Tarun 描述的版本。

$ARGS provides access to named ( --arg name value ) and positional ( --args one two three ) arguments from the jq command line, and allows you to build up objects easily & safely. $ARGS提供从 jq 命令行访问命名( --arg name value )和位置( --args one two three )参数,并允许您轻松安全地构建对象。

Named arguments:命名参数:

$ jq -n '{christmas: $ARGS.named}' \
  --arg one 'partridge in a "pear" tree' \
  --arg two 'turtle doves'

{
  "christmas": {
    "one": "partridge in a \"pear\" tree",
    "two": "turtle doves"
  }
}

Positional arguments:位置参数:

$ jq -n '{numbers: $ARGS.positional}' --args 1 2 3

{
  "numbers": [
    "1",
    "2",
    "3"
  ]
}

Note you can access individual items of the positional array, and that the named arguments are directly available as variables:请注意,您可以访问位置数组的各个项目,并且命名参数可直接用作变量:

jq -n '{first: {name: $one, count: $ARGS.positional[0]}, all: $ARGS}' \
  --arg one 'partridge in a "pear" tree' \
  --arg two 'turtle doves' \
  --args 1 2 3

{
  "first": {
    "name": "partridge in a \"pear\" tree",
    "count": "1"
  },
  "all": {
    "positional": [
      "1",
      "2",
      "3"
    ],
    "named": {
      "one": "partridge in a \"pear\" tree",
      "two": "turtle doves"
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM