我的JSON输入文件如下:
{ "Name":"SA", "Password":"yyyyy", "Mappings" : { "RegionMap" : { "us-east-1" : { "AMI" : "xxxxxx" }, "us-east-2" : { "AMI" : "" }, "us-west-1" : { "AMI" : "" }, "us-west-2" : { "AMI" : "" }, "ca-central-1" : { "AMI" : "" }, "eu-central-1" : { "AMI" : "" }, "eu-west-1" : { "AMI" : "" }, "eu-west-2" : { "AMI" : "" }, "ap-south-1" : { "AMI" : "" }, "ap-southeast-1" : { "AMI" : "" }, "ap-southeast-2" : { "AMI" : "" }, "ap-northeast-1" : { "AMI" : "" }, "ap-northeast-2" : { "AMI" : "" }, "sa-east-1" : { "AMI" : "" } } } }我的任务是更新给定json文件中us-east-1的AMI键的值,并将更新后的文件创建为新文件.我正在使用带有重定向运算符的 jq 插件.它具有 + 运算符,可用于overwrite对象中的内容.
My task is to update the value of the AMI key of us-east-1 in the given json file and create the updated file as new file. I am using the jq plugin with redirection operator. It has + operator which can used to overwrite contents in an object.
我的expected输出是
{ "Name":"SA", "Password":"yyyyy", "Mappings" : { "RegionMap" : { "us-east-1" : { "AMI" : "abcd" }, "us-east-2" : { "AMI" : "" }, "us-west-1" : { "AMI" : "" }, "us-west-2" : { "AMI" : "" }, "ca-central-1" : { "AMI" : "" }, "eu-central-1" : { "AMI" : "" }, "eu-west-1" : { "AMI" : "" }, "eu-west-2" : { "AMI" : "" }, "ap-south-1" : { "AMI" : "" }, "ap-southeast-1" : { "AMI" : "" }, "ap-southeast-2" : { "AMI" : "" }, "ap-northeast-1" : { "AMI" : "" }, "ap-northeast-2" : { "AMI" : "" }, "sa-east-1" : { "AMI" : "" } } } }我当前的命令未提供预期的输出,其结果如下:
My current command is not providing the expected output and its as follows:
jq '.Mappings.RegionMap + { "us-east-1":{"AMI":"abcd"}}' <OldfileName> > <Newfilename>有人可以帮助我使用重定向运算符实现预期的输出吗?
Can somebody help me with achieving the expected output using redirection operator ?
我在cygwin环境上运行命令,而我的 jq 版本是.
I am running the command on a cygwin environment and my jq version is 1.5 .
编辑
在密码密钥后添加了逗号
Added a commas after the Password Key
推荐答案使输入json无效的第一个问题是此行(第三行):
The first issue that makes your input json invalid is this line (the 3rd line):
... "Password":"yyyyy" <---- ...应紧跟,.
修复此问题后,您可以轻松更新所需的属性值:
After fixing that, you can easily update the needed property value:
jq '.Mappings.RegionMap["us-east-1"].AMI = "abcd"' oldfile > newfile
newfile内容:
newfile contents:
{ "Name": "SA", "Password": "yyyyy", "Mappings": { "RegionMap": { "us-east-1": { "AMI": "abcd" }, "us-east-2": { "AMI": "" }, "us-west-1": { "AMI": "" }, "us-west-2": { "AMI": "" }, "ca-central-1": { "AMI": "" }, "eu-central-1": { "AMI": "" }, "eu-west-1": { "AMI": "" }, "eu-west-2": { "AMI": "" }, "ap-south-1": { "AMI": "" }, "ap-southeast-1": { "AMI": "" }, "ap-southeast-2": { "AMI": "" }, "ap-northeast-1": { "AMI": "" }, "ap-northeast-2": { "AMI": "" }, "sa-east-1": { "AMI": "" } } } }更多推荐
使用重定向运算符通过jq更新文件内容
发布评论