使用jq基于第三个json更新一个json文件和第二个json

时间:2017-07-06 16:49:38

标签: 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"
  }
}

input.json:

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

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:

{
  ".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实现这个目标?

1 个答案:

答案 0 :(得分:1)

你没有详细说明你想要的转变,但如果你能指定算法,那么请放心,它可以在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 builtins getpath和{一起使用的路径规范{1}}。

例如,请考虑keyfile.json的这种格式:

setpath

因此,与您的示例对应的前两个条目将是:

[ [<path in input.json>], [<path in vault.json> ], ... ]

使用&#34;金库&#34;作为更新的基础,您的jq程序将成为单行:

[
  [ [".cloud_cpi_key","value"], ["aws","access_key_id"]],
  [ [".cloud_cpi_secret"], ["aws", "secret_access_key"]]
]