使用jq将新的密钥对附加到JSON文件 [英] Append new keypair to JSON file with jq

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

问题描述

在相同的一般前提下,我已经阅读了几乎所有可以在SO上找到的问题,并且已经接近三个不同的时间,但是我无法完成这项工作.

I've read almost every question I can find on SO with the same general premise and I've gotten close three different times, but I just can't make this work.

给出一个名为parameters.json的JSON文件,并带有如下所示的密钥对: (出于共享目的而缩短)

Given a JSON file called parameters.json with keypairs like so; (shortened for sharing purposes)

[
  {
    "ParameterKey": "Foo1",
    "ParameterValue": "Bar1"
  },
  {
    "ParameterKey": "Foo2",  
    "ParameterValue": "Bar2"
  }
]

我想添加另一个密钥对,

I want to add another keypair, say

  {
    "ParameterKey": "Foo3",
    "ParameterValue": "Bar3"
  }

所以我最终得到一个像

[
  {
    "ParameterKey": "Foo1",
    "ParameterValue": "Bar1"
  },
  {
    "ParameterKey": "Foo2",  
    "ParameterValue": "Bar2"
  },
  {
    "ParameterKey": "Foo3",
    "ParameterValue": "Bar3"
  }
]

用例和尝试

我正在编写一个bash脚本,用于复制和更新AWS的参数文件-这是密钥对的来源.我们有一个新的密钥对,当复制它们时,需要将其添加到任何现有文件中.我可以把if/then做得很好,实际上是添加了让我感到困扰的新密钥对.

Use Case & Attempts

I'm writing a bash script that copies and updates parameter files for AWS - this is where the keypairs come in. We have a new keypair that needs to be added to any existing files when they're copied. I can get the if/then done fine, it's physically adding the new keypair that is stumping me.

经过大量的搜寻,我最终得到了这个笨拙的解决方法,我不太了解,尽管它可以很好地生成密钥对,但也不会将其重新添加到parameters.json

With a ton of googling I ended up with this clunky workaround, which I don't really understand and, while it generates the keypair just fine, doesn't add it back into parameters.json

item='{"ParameterKey": "RTSMSnapshotID"}'

jq --argjson item "$item" '$item + {"ParameterValue": ""}' parameters.json

我在命令行上正确地将密钥对打印回了我,因此我想添加| sponge parameters.json,但这会给我一个空文件.

I get the keypair printed back to me correctly on the command line, so I figure add | sponge parameters.json but that then gives me an empty file.

我还考虑了sed解决方法,该方法可修整文件的最后一行,然后附加新的密钥对(和EOF),但是sed当然不能与换行符配合使用,因此我无法使其正常工作.

I also considered a sed workaround that trimmed the last line of the file and then appended the new keypair (and EOF) but of course sed doesn't play well with newlines and I can't get it to work.

我愿意接受在bash中使用任何东西而在jq中使用任何东西的解决方案.我仍然是bash/jq菜鸟,因此请您详细解释.

I'm open to solutions that use anything in bash and anything in jq. I am still a bash/jq noob so detailed explanations are appreciated.

推荐答案

使用argjson的想法是正确的,但是语法如下所示jq-1.5

The idea of using argjson is right, but the syntax is as below on jq-1.5

jq --argjson obj '{ "ParameterKey": "Foo3", "ParameterValue": "Bar3" }' '. + [$obj]' < json 
[
  {
    "ParameterKey": "Foo1",
    "ParameterValue": "Bar1"
  },
  {
    "ParameterKey": "Foo2",
    "ParameterValue": "Bar2"
  },
  {
    "ParameterKey": "Foo3",
    "ParameterValue": "Bar3"
  }
]

检查 jq文档以获得有关+运算符.

Check the jq-documentation for more information about the + and += operators.

我不知道jq本身的方式,但是您可以使用整洁的bash技巧来做到这一点,

Am not aware of ways to do this in jq itself, but you can use a neat bash trick to do this,

jq --argjson obj '{ "ParameterKey": "Foo3", "ParameterValue": "Bar3" }' '. + [$obj]' < json > temp && mv temp json

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

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