使用jq在第三个json的基础上使用第二个json更新一个json文件
我有三个JSON文件:使用jq在第三个json的基础上使用第二个json更新一个json文件,json,dictionary,merge,jq,Json,Dictionary,Merge,Jq,我有三个JSON文件: vault.json: { "aws": { "access_key_id": "My-Key-id", "secret_access_key": "My-Access-Key" }, "ssl": { "crt": "My-Crt", "key": "My-Key", "req": "My-Req" } } { ".cloud_cpi_key": { "type": "wildcard_dom
vault.json:
{
"aws":
{
"access_key_id": "My-Key-id",
"secret_access_key": "My-Access-Key"
},
"ssl":
{
"crt": "My-Crt",
"key": "My-Key",
"req": "My-Req"
}
}
{
".cloud_cpi_key": {
"type": "wildcard_domain",
"configurable": true,
"credential": false,
"value": "vault-supplied-value",
"optional": false
},
".cloud_cpi_secret": {
"type": "wildcard_domain",
"configurable": true,
"credential": false,
"value": "vault-supplied-value",
"optional": false
},
".properties.networking_point_of_entry": {
"type": "selector",
"configurable": true,
"credential": false,
"value": "external_ssl",
"optional": false
},
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate": {
"type": "rsa_cert_credentials",
"configurable": true,
"credential": true,
"value": {
"private_key_pem": "vault-supplied-value",
"cert_pem": "vault-supplied-value"
},
"optional": false
}
}
{
".cloud_cpi_key": {
"type": "string",
"configurable": true,
"credential": true,
"value": "My-Key-id",
"optional": false
},
".cloud_cpi_secret": {
"type": "string",
"configurable": true,
"credential": true,
"value": "My-Access-Key",
"optional": false
},
".properties.networking_point_of_entry": {
"type": "selector",
"configurable": true,
"credential": false,
"value": "external_ssl",
"optional": false
},
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate": {
"type": "rsa_cert_credentials",
"configurable": true,
"credential": true,
"value": {
"private_key_pem": "My-Key",
"cert_pem": "My-Crt"
},
"optional": false
}
}
input.json:
{
"aws":
{
"access_key_id": "My-Key-id",
"secret_access_key": "My-Access-Key"
},
"ssl":
{
"crt": "My-Crt",
"key": "My-Key",
"req": "My-Req"
}
}
{
".cloud_cpi_key": {
"type": "wildcard_domain",
"configurable": true,
"credential": false,
"value": "vault-supplied-value",
"optional": false
},
".cloud_cpi_secret": {
"type": "wildcard_domain",
"configurable": true,
"credential": false,
"value": "vault-supplied-value",
"optional": false
},
".properties.networking_point_of_entry": {
"type": "selector",
"configurable": true,
"credential": false,
"value": "external_ssl",
"optional": false
},
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate": {
"type": "rsa_cert_credentials",
"configurable": true,
"credential": true,
"value": {
"private_key_pem": "vault-supplied-value",
"cert_pem": "vault-supplied-value"
},
"optional": false
}
}
{
".cloud_cpi_key": {
"type": "string",
"configurable": true,
"credential": true,
"value": "My-Key-id",
"optional": false
},
".cloud_cpi_secret": {
"type": "string",
"configurable": true,
"credential": true,
"value": "My-Access-Key",
"optional": false
},
".properties.networking_point_of_entry": {
"type": "selector",
"configurable": true,
"credential": false,
"value": "external_ssl",
"optional": false
},
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate": {
"type": "rsa_cert_credentials",
"configurable": true,
"credential": true,
"value": {
"private_key_pem": "My-Key",
"cert_pem": "My-Crt"
},
"optional": false
}
}
keyfile.json
{
".cloud_cpi_key.value": "aws.access_key_id",
".cloud_cpi_secret": "secret_access_key",
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate.value.private_key_pem": "ssl.key",
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate.value.cert_pem": "ssl.crt"
}
我想根据第三个json,用第一个json的值更新第二个json文件。
这可以通过JQ来提供output.json吗
output.json:
{
"aws":
{
"access_key_id": "My-Key-id",
"secret_access_key": "My-Access-Key"
},
"ssl":
{
"crt": "My-Crt",
"key": "My-Key",
"req": "My-Req"
}
}
{
".cloud_cpi_key": {
"type": "wildcard_domain",
"configurable": true,
"credential": false,
"value": "vault-supplied-value",
"optional": false
},
".cloud_cpi_secret": {
"type": "wildcard_domain",
"configurable": true,
"credential": false,
"value": "vault-supplied-value",
"optional": false
},
".properties.networking_point_of_entry": {
"type": "selector",
"configurable": true,
"credential": false,
"value": "external_ssl",
"optional": false
},
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate": {
"type": "rsa_cert_credentials",
"configurable": true,
"credential": true,
"value": {
"private_key_pem": "vault-supplied-value",
"cert_pem": "vault-supplied-value"
},
"optional": false
}
}
{
".cloud_cpi_key": {
"type": "string",
"configurable": true,
"credential": true,
"value": "My-Key-id",
"optional": false
},
".cloud_cpi_secret": {
"type": "string",
"configurable": true,
"credential": true,
"value": "My-Access-Key",
"optional": false
},
".properties.networking_point_of_entry": {
"type": "selector",
"configurable": true,
"credential": false,
"value": "external_ssl",
"optional": false
},
".properties.networking_point_of_entry.external_ssl.ssl_rsa_certificate": {
"type": "rsa_cert_credentials",
"configurable": true,
"credential": true,
"value": {
"private_key_pem": "My-Key",
"cert_pem": "My-Crt"
},
"optional": false
}
}
我可以用我喜欢的任何方式修改keyfile.json,使事情变得更简单,比如
{
"fromkey": "aws.access_key_id"
"tokey": ".cloud_cpi_key.value"
},
{ "fromkey": ....
}
但是keyfile.json中不能放置任何值,只能放置键名
我可以修改vault.json,将内容放入数组中,或者您拥有的其他内容,但我无法更改最低级别,即我无法更改:
{
"access_key_id": "My-Key-id",
"secret_access_key": "My-Access-Key"
}
我无法修改input.json。
我如何使用JQ实现这一点?您没有给出您心目中的转换的详细信息,但是如果您可以指定算法,那么请放心,它可以在JQ中完成 也许您缺少的是如何让jq读取这三个文件。继续的一种方法是使用调用:
jq --argfile keyfile keyfile.json --argfile vault vault.json -f vault.jq input.json
其中,vault.jq是包含jq程序的文件,在该文件中,您可以将keyfile.json的内容称为$keyfile
,类似地,也可以将其称为vault.json的内容
简化keyfile.json
由于您表示您对keyfile.json的格式有一定的灵活性,而且它似乎包含路径信息,因此我建议您考虑采用可直接用于jq内置getpath
和setpath
的路径规范
例如,考虑KiFiel.JSON:< /P>的格式
[ [<path in input.json>], [<path in vault.json> ], ... ]
要使用“保险库”作为更新的基础,您的jq计划将是一行:
reduce $keyfile[] as $p (.; setpath(($p|.[0]); $vault|getpath($p|.[1])))
这是完美的,我从未对JQ失去信心。我手工制作密钥文件,因此按照您给出的示例进行改造将非常有效!再次感谢!