使用jq创建JSON文件 [英] Create JSON file using jq

查看:100
本文介绍了使用jq创建JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过执行以下命令来创建JSON文件:

I'm trying to create a JSON file by executing the following command:

jq --arg问候世界'{"hello":"$ greeting"}''>file.json

该命令被卡住,没有任何输入.虽然

This command stuck without any input. While

jq -n --arg问候世界'{"hello":"$ greeting"}''>file.json 无法正确解析.我只是想知道是否真的可以创建JSON文件.

jq -n --arg greeting world '{"hello":"$greeting"}' > file.json doesn't parse correctly. I'm just wondering is really possible to create a JSON file.

推荐答案

因此您的代码无法正常工作,因为在双引号中包含了变量,该变量被视为字符串.这就是为什么它不起作用

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

正如@Jeff Mercado所指出的,解决方案是

As @Jeff Mercado, pointed out the solution is

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

关于名称中的-.这实际上是可能的.但是到目前为止,在jq的发行版本中尚不可用.如果在系统上编译jq的master分支.有一个名为 $ ARGS.named 的新变量,可用于访问信息.

About the - in a name. This is actually possible. But as of now this is not available in released version of jq. If you compile the master branch of jq on your system. There is a new variable called $ARGS.named which can be used to access the information.

我刚刚编译并检查了以下命令,它就像一个超级魅力

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"
}

这篇关于使用jq创建JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆