使用jq基于第三个JSON的第二个JSON更新一个JSON数组元素

时间:2017-08-02 23:29:17

标签: json dictionary merge jq

我有四个文件:

input.json

{
  "appd": {
    "account_key": "appd-key",
    "account_name": "appd-user"
  },
  "aws": {
    "access_key_id": "my-key-id",
    "region": "us-east-1",
    "secret_access_key": "my-secret-key"
  }
}

vault.json

[
  [[".properties.on_demand_service_plans_collection", "value[0]", "account_name"], ["appd", "account_name"]],
  [[".properties.on_demand_service_plans_collection", "value[0]", "account_access_key"], ["appd", "account_key"]],
  [[".properties.on_demand_service_plans_collection", "value[1]", "account_name"], ["appd", "account_name"]],
  [[".properties.on_demand_service_plans_collection", "value[1]", "account_access_key"], ["appd", "account_key"]]
]

keyfile.json

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

valut.jq

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

这是我的jq命令:

{
  ".properties.on_demand_service_plans_collection": {
    "type": "collection",
    "value": [
      {
        "plan_name": "my-plan",
        "plan_description": "my-plan",
        "account_name": "appd-key",
        "account_access_key": "appd-user"
      },
      {
        "plan_name": "my-plan-test",
        "plan_description": "my-plan-test",
        "account_name": "appd-key",
        "account_access_key": "appd-user",
        "controller_host": "example.com"
      }
    ],
    "optional": false
  }
}

desired.json

{
  ".properties.on_demand_service_plans_collection": {
    "type": "collection",
    "value": [
      {
        "plan_name": "my-plan",
        "plan_description": "my-plan",
        "account_name": "vault-supplied-value",
        "account_access_key": "vault-supplied-value"
      },
      {
        "plan_name": "my-plan-test",
        "plan_description": "my-plan-test",
        "account_name": "vault-supplied-value",
        "account_access_key": "vault-supplied-value"
      }
    ],
    "optional": false,
    "value[0]": {
      "account_name": "appd-user",
      "account_access_key": "appd-key"
    },
    "value[1]": {
      "account_name": "appd-user",
      "account_access_key": "appd-key"
    }
  }
}

我没有正确编码keyfile.json文件,因为我没有得到想要的结果。 我已经尝试了我能想到的keyfile.json的每一次迭代,但无济于事。

有没有人有任何想法? 提前谢谢。

什么-I-get.json

<div class="bgimg" id="background">

1 个答案:

答案 0 :(得分:2)

我认为它只是密钥文件中使用的语法 - 它导致创建字符串文字"value[0]"而不是路径替换中引用的数组索引0。

尝试使用语法

"value",0,"account_name"

而不是

"value[0]","account_name"

使用以下keyfile.json

[
  [["properties.on_demand_service_plans_collection", "value",0, "account_name"], ["appd", "account_name"]],
  [["properties.on_demand_service_plans_collection", "value",0, "account_access_key"], ["appd", "account_key"]],
  [["properties.on_demand_service_plans_collection", "value",1, "account_name"], ["appd", "account_name"]],
  [["properties.on_demand_service_plans_collection", "value",1, "account_access_key"], ["appd", "account_key"]]
]


产生desired.json:

{
  ".properties.on_demand_service_plans_collection": {
    "type": "collection",
    "value": [
      {
        "plan_name": "my-plan",
        "plan_description": "my-plan",
        "account_name": "appd-user",
        "account_access_key": "appd-key"
      },
      {
        "plan_name": "my-plan-test",
        "plan_description": "my-plan-test",
        "account_name": "appd-user",
        "account_access_key": "appd-key"
      }
    ],
    "optional": false
  }
}

<强>旁注: 如果它有帮助,我在一些头部划分之后遇到正确语法的方式是将输出视为紧凑格式的流:

jq -c '. | tostream' input.json
[[".properties.on_demand_service_plans_collection","optional"],false]
[[".properties.on_demand_service_plans_collection","type"],"collection"]
[[".properties.on_demand_service_plans_collection","value",0,"account_access_key"],"vault-supplied-value"]
[[".properties.on_demand_service_plans_collection","value",0,"account_name"],"vault-supplied-value"]
[[".properties.on_demand_service_plans_collection","value",0,"plan_description"],"my-plan"]
[[".properties.on_demand_service_plans_collection","value",0,"plan_name"],"my-plan"]
[[".properties.on_demand_service_plans_collection","value",0,"plan_name"]]
[[".properties.on_demand_service_plans_collection","value",1,"account_access_key"],"vault-supplied-value"]
[[".properties.on_demand_service_plans_collection","value",1,"account_name"],"vault-supplied-value"]
[[".properties.on_demand_service_plans_collection","value",1,"plan_description"],"my-plan-test"]
[[".properties.on_demand_service_plans_collection","value",1,"plan_name"],"my-plan-test"]
[[".properties.on_demand_service_plans_collection","value",1,"plan_name"]]
[[".properties.on_demand_service_plans_collection","value",1]]
[[".properties.on_demand_service_plans_collection","value"]]
[[".properties.on_demand_service_plans_collection"]]