# List of nested Key-value pairs

**URL:** <https://discuss.hashicorp.com/t/list-of-nested-key-value-pairs/44417>\
**Category:** Consul\
**Tags:** consul-template\
**Created:** [September 16, 2022, 7:54pm UTC](https://discuss.hashicorp.com/t/list-of-nested-key-value-pairs/44417 "2022-09-16T19:54:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jalexander](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jalexander/32/24100_2.png) [@jalexander](https://discuss.hashicorp.com/u/jalexander)\
**Post date:** [September 16, 2022, 7:54pm UTC](https://discuss.hashicorp.com/t/list-of-nested-key-value-pairs/44417/1 "2022-09-16T19:54:20Z")

</div>

Hey! I need some figuring out how process a nested list in Consul’s Key-value store with Consul Template.

My data is stored this way:  
kv/service/data/config/process/servers/0/name → string\_value  
kv/service/data/config/process/servers/0/enabled → boolean\_value

kv/service/data/config/process/servers/1/name → string\_value  
kv/service/data/config/process/servers/1/enabled → boolean\_value

I’ve followed Consul-template documentation but for the life of me, I can’t seem to get my head around how to retrieve these nested values.

If possible, I’d like to generate an output like the following:

```auto
service:
  data:
    config:
      process:
        servers:
          0:
            name: <server_name>
            enabled: <boolean>
          1:
            name: <server_name>
            enabled: <boolean>

```

Is this possible to achieve with Consul-template?

---

<div class="post-metadata">

**Author:** ![eikenb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/eikenb/32/1173_2.png) [@eikenb](https://discuss.hashicorp.com/u/eikenb)\
**Post date:** [September 19, 2022, 9:48pm UTC](https://discuss.hashicorp.com/t/list-of-nested-key-value-pairs/44417/2 "2022-09-19T21:48:40Z")

</div>

Hey @jalexander, thanks for asking.

What you want should be possible but how requires a quick initial question… Those entries under `servers:`, are they fixed so there is always a 0 and 1 (with name/enabled) or is that a variable length list of servers (0…N) that should be generated when the template is run?

---

<div class="post-metadata">

**Author:** ![jalexander](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jalexander/32/24100_2.png) [@jalexander](https://discuss.hashicorp.com/u/jalexander)\
**Post date:** [September 20, 2022, 1:19am UTC](https://discuss.hashicorp.com/t/list-of-nested-key-value-pairs/44417/3 "2022-09-20T01:19:50Z")

</div>

> [@eikenb](#):
>
> variable length list of servers (0…N)

Thanks for your response. (0…N) as its possible it will be initialized with an empty list if there are no servers.

---

<div class="post-metadata">

**Author:** ![jalexander](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jalexander/32/24100_2.png) [@jalexander](https://discuss.hashicorp.com/u/jalexander)\
**Post date:** [September 20, 2022, 6:38am UTC](https://discuss.hashicorp.com/t/list-of-nested-key-value-pairs/44417/4 "2022-09-20T06:38:24Z")

</div>

Managed to figure it out… Here’s the approach I took:

```auto
servers:
  {{ range $key, $pairs := tree "service/data/config/process/servers/owners" | byKey }}{{ $key }}:
    {{ range $pair := $pairs}}
       {{$pair.Key}}:
         {{$pair.Value}}
  {{ end }}{{ end }}

```

---

<div class="post-metadata">

**Author:** ![eikenb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/eikenb/32/1173_2.png) [@eikenb](https://discuss.hashicorp.com/u/eikenb)\
**Post date:** [September 20, 2022, 6:35pm UTC](https://discuss.hashicorp.com/t/list-of-nested-key-value-pairs/44417/5 "2022-09-20T18:35:29Z")

</div>

😄 Exactly the same code that I came up with but you beat me to it.  
Glad you figured it out!
