使用jq根据基于第三个json的第二个json更新一个json文件 [英] update one json file with a second json based on third json with jq

查看:134
本文介绍了使用jq根据基于第三个json的第二个json更新一个json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个JSON文件:

I have three JSON files:

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吗?

I'd like to update the second json file, with the values from the first json, based on the third json. can this be done via JQ to provide 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,例如

I can modify keyfile.json any way I like to make things easier, like

{
    "fromkey": "aws.access_key_id"
    "tokey": ".cloud_cpi_key.value"
},
{   "fromkey": ....
}

但是在keyfile.json中不能放置任何值,只能在键名中放置

But no values may be placed in keyfile.json, only key names.

我可以修改vault.json以将其放入数组或具有什么功能,但是我不能更改最低级别,即我不能更改:

And I can modify vault.json, to put things in arrays, or what-have-you, but I cannot change the lowest levels, i.e. I cannot change:

{
    "access_key_id": "My-Key-id",
    "secret_access_key": "My-Access-Key"
}

我无法修改input.json. 我如何用JQ做到这一点?

I cannot modify input.json. How can I accomplish this with JQ?

推荐答案

您没有提供有关转换概念的详细信息,但是如果可以指定算法,那么请放心,可以在jq中完成.

You don't give details about the transformation you have in mind, but if you can specify the algorithm, then rest assured, it can be done in jq.

也许您缺少的是如何让jq读取三个文件.一种处理方式是使用调用:

Perhaps the thing you're missing is how to get jq to read the three files. One way to proceed would be to use the invocation:

jq --argfile keyfile keyfile.json --argfile vault vault.json -f vault.jq input.json

其中vault.jq是包含jq程序的文件,其中您将keyfile.json的内容称为$keyfile,类似地,将vault.json的内容称为

where vault.jq is the file containing your jq program, in which you would refer to the contents of keyfile.json as $keyfile, and similarly for the contents of vault.json

由于您表示对keyfile.json的格式具有一定的灵活性,并且由于它似乎包含路径信息,因此我建议考虑采用可直接与jq内置文件getpathsetpath一起使用的路径规范.

Since you indicate you have some flexibility about the format of keyfile.json, and since it appears to hold path information, I would recommend considering adopting path specifications that can be used directly with the jq builtins getpath and setpath.

例如,为keyfile.json考虑以下格式:

For example, consider this format for keyfile.json:

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

与您的示例相对应的前两个条目将是:

The first two entries corresponding to your example would thus be:

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

要使用库"作为更新的基础,则您的jq程序将成为一线软件:

To use the "vault" as the basis for updates, your jq program would then be the one-liner:

reduce $keyfile[] as $p (.; setpath(($p|.[0]); $vault|getpath($p|.[1])))

这篇关于使用jq根据基于第三个json的第二个json更新一个json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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