# Combine two objects

**URL:** <https://discuss.hashicorp.com/t/combine-two-objects/42153>\
**Category:** Terraform\
**Created:** [July 19, 2022, 9:56am UTC](https://discuss.hashicorp.com/t/combine-two-objects/42153 "2022-07-19T09:56:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr-nsh](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mr-nsh/32/29518_2.png) [@mr-nsh](https://discuss.hashicorp.com/u/mr-nsh)\
**Post date:** [July 19, 2022, 9:56am UTC](https://discuss.hashicorp.com/t/combine-two-objects/42153/1 "2022-07-19T09:56:55Z")

</div>

Hi all. I wanted to ask you for some help.

I have several maps of objects:

```auto
locals {
  obj_a = {
    table_name1 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address = "127.0.0.1"
        },
        {
          destination_prefix = "127.0.0.2/24"
          next_hop_address = "127.0.0.2"
        },
       {...}
      table_name2 = {...}

locals {
  obj_b = {
    table_name3 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address = "127.0.0.1"
        },
        {...}
      table_name4 = {...}

```

is there any possibility to combine this in one variable? something like that:

```auto
module "route_tables" {
  source = "git@"

  table = local.obj_a, local.obj_b
}

```

function **concat** did not help ☹

Thanks in advance

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [July 19, 2022, 3:10pm UTC](https://discuss.hashicorp.com/t/combine-two-objects/42153/2 "2022-07-19T15:10:40Z")

</div>

Hi @mr-nsh,

The answer here will depend on what exactly you mean by “combine”. The `concat` function can combine lists in the sense of concatenating them together in the given order, but that doesn’t make sense for maps which have no order.

Can you show an example of what you’d expect the final value to look like after being combined in the way you imagine? Then we can talk about ways to get that result automatically, instead of hard-coding it.

---

<div class="post-metadata">

**Author:** ![mr-nsh](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mr-nsh/32/29518_2.png) [@mr-nsh](https://discuss.hashicorp.com/u/mr-nsh)\
**Post date:** [July 19, 2022, 6:27pm UTC](https://discuss.hashicorp.com/t/combine-two-objects/42153/3 "2022-07-19T18:27:18Z")

</div>

Final value as a sequence or union of the contents of two or more local.obj\_#

those. I can’t figure out how to pass multiple local.obj as value var.table

Thanks.

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [July 20, 2022, 12:40am UTC](https://discuss.hashicorp.com/t/combine-two-objects/42153/4 "2022-07-20T00:40:57Z")

</div>

Hi @mr-nsh,

Please write out the exact value you intend to produce using the Terraform language syntax. Then I can tell you one or more options for how to produce that result in terms of `local.obj_a` and `local.obj_b`.

---

<div class="post-metadata">

**Author:** ![mr-nsh](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mr-nsh/32/29518_2.png) [@mr-nsh](https://discuss.hashicorp.com/u/mr-nsh)\
**Post date:** [July 20, 2022, 6:35am UTC](https://discuss.hashicorp.com/t/combine-two-objects/42153/5 "2022-07-20T06:35:21Z")

</div>

Hi @apparentlymart ,

it would be convenient to just list local.obj separated by commas so that the module would process them one by one:

```auto
table = local.obj_a, local.obj_b

```

but if we need to form a third object, then it should look like this:

```auto
locals {
  obj_result = {
    table_name1 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address = "127.0.0.1"
        },
        {
          destination_prefix = "127.0.0.2/24"
          next_hop_address = "127.0.0.2"
        },
       {...}
      table_name2 = {...}
      table_name3 = {
      description = ""
      routes = [
        {
          destination_prefix = "127.0.0.1/24"
          next_hop_address = "127.0.0.1"
        },
        {...}
      table_name4 = {...}
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![kingfisher77](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/kingfisher77/32/34537_2.png) [@kingfisher77](https://discuss.hashicorp.com/u/kingfisher77)\
**Post date:** [March 15, 2023, 11:30am UTC](https://discuss.hashicorp.com/t/combine-two-objects/42153/6 "2023-03-15T11:30:27Z")

</div>

Did you solve this task? We have almost the same challenge.
