Search code examples
arraysjsonpowershelljqhashicorp-vault

How can I use jq to get the key value pairs only and set as environment variable?


Below is the vault output generated by the vault command

vault kv get kv/Env | jq

{
  "request_id": "8498e5e4-050a-51e9-deaf-64a06a0",
  "lease_id": "",
  "lease_duration": 0,
  "renewable": false,
  "data": {
    "data": {
      "test": "test123",
      "test1": "test1234"
    },
    "metadata": {
      "created_time": "2022-11-17T12:2214.93229792Z",
      "custom_metadata": null,
      "deletion_time": "",
      "destroyed": false,
      "version": 1
    }
  },
  "warnings": null
}

I am unable to find a way to get only test and test1 and set as environmental variable in powershell. The issue is I would like to extract only test and test1, but it is under data/data which is like sublist and unable to find a way to extract it. Kindly help

I had provided the paths to delete as below but not sure if its efficient way to loop through

vault kv get kv/Env | jq 'del(.data.metadata, .request_id, .lease_id, .lease_duration, .renewable, .warnings)'

I would like to get the output something like below which I can loop through and set as variable.

test=test123
test1=test1234

Solution

  • You can acess the nested object with .data.data. You can convert an object into a list of key-value pairs with to_entries. Then it is only a matter of joining:

    vault ... | jq -r '.data.data | to_entries[] | join("=")'
    

    Output:

    test=test123
    test1=test1234