1

I have four files:

input.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
  }
}

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

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"]]
]

valut.jq

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

This is my jq command:

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

desired.json

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

I'm not coding the keyfile.json file correctly, because I am not getting the desired results. I've tried every iteration of the keyfile.json I can think of, but to no avail.

Does anyone have any ideas? Thanks in advance.

what-i-get.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"
    }
  }
}
1

1 Answer 1

2

I believe it's just the syntax used in your keyfile - It is causing a string literal "value[0]" to be created instead of the array index 0 being referenced in the path substitution.

Try use the syntax

"value",0,"account_name"

Instead of

"value[0]","account_name"

Using the below as 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"]]
]


Produces 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
  }
}

Sidenote: If it helps, the way I came across the proper syntax after some headscratching was to view the output as a stream in compact format:

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"]]
Sign up to request clarification or add additional context in comments.

1 Comment

The one iteration I did not try! Thank you @hmedia1 for this perfect answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.