简体   繁体   English

使用JQ使用来自另一个JSON的值更新一个JSON文件值

[英]Update one JSON file values with values from another JSON using JQ

So I have two JSON files: 所以我有两个JSON文件:

bosh.json:

{
  "key_pair_name": "my-aws-keypair",
  "ssh_private_key": "my-key-name",
  "trusted_certificates": "my-trusted-certs"
}

model.json:

{
  "trusted_certificates": "vault-supplied-value",
  "vm_password_type": "generate"
}

and I want to update the model.json file with the bosh.json file so it looks like this: 而且我想用bosh.json文件更新model.json文件,所以看起来像这样:

{
  "trusted_certificates": "my-trusted-certs",
  "vm_password_type": "generate"
}

I tried this: 我尝试了这个:

jq --argfile bosh bosh.json '. += $bosh' model.json

but I get too many keys 但是我钥匙太多

{
  "trusted_certificates": "my-trusted-certs",
  "vm_password_type": "generate",
  "key_pair_name": "my-aws-keypair",
  "ssh_private_key": "my-key-name"
}

and

jq --argfile bosh bosh.json '. + $bosh' model.json

yeilds the same... 一样...

{
  "trusted_certificates": "my-trusted-certs",
  "vm_password_type": "generate",
  "key_pair_name": "my-aws-keypair",
  "ssh_private_key": "my-key-name"
}

while this 而这

jq --argfile bosh bosh.json '. = $bosh' model.json

yields the incorrect keys... 产生不正确的键...

{
  "key_pair_name": "my-aws-keypair",
  "ssh_private_key": "my-key-name",
  "trusted_certificates": "my-trusted-certs"
}

Does anyone have any ideas how to get the expected results using jq? 有谁知道如何使用jq获得预期结果? by the way, I cannot use the value of the key for the updates, as i will have unexpected results in other permutations... 顺便说一句,我不能使用键的值来进行更新,因为在其他排列中我会得到意想不到的结果...

jq solution: jq解决方案:

jq --argfile bosh bosh.json 'with_entries( 
         if $bosh[.key] then .value = $bosh[.key] else . end)' model.json

The output: 输出:

{
  "trusted_certificates": "my-trusted-certs",
  "vm_password_type": "generate"
}

  • if $bosh[.key] then .value = $bosh[.key] else . end if $bosh[.key] then .value = $bosh[.key] else . end - update model 's value only for matched keys if $bosh[.key] then .value = $bosh[.key] else . end仅针对匹配的键更新模型的值

The requirements are not completely clear, but here's the solution to one interpretation. 要求尚不完全清楚,但这是一种解释的解决方案。 This solution can easily be modified to match the other obvious interpretation. 可以轻松修改此解决方案以匹配其他明显的解释。

Assuming the file bosh.jq contains the following jq program: 假设文件bosh.jq包含以下jq程序:

reduce keys[] as $k (.; if $bosh|has($k) then .[$k] = $bosh[$k] else . end)

then the command: 然后命令:

jq --argfile bosh bosh.json -f bosh.jq model.json

will in effect emit the edited version of model.json. 实际上将发出model.json的编辑版本。

Using with_entries 使用with_entries

If you prefer a reduce-free approach, consider: 如果您希望采用无减少量的方法,请考虑:

with_entries(.key as $k | if $bosh|has($k) then .value = $bosh[$k] else . end )

Note that if $bosh|has($k) ... is NOT the same as if $bosh[$k] ... . 注意, if $bosh|has($k) ...if $bosh[$k] ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM