4

I have a json file as given below

{
   "Domain": {
    "Services": [{
        "service1": [{
            "custid" : "1104",
            "fname" : "ton",
            "lname" : "hatf",
        }],
        "service2": [{
            "custid" : "1105",
            "fname" : "ran",
            "lname" : "ttt",
        }],
        "service3": [{
            "custid" : "1106",
            "fname" : "rin",
            "lname" : "wqg",
        }]
    }]
}
 }

Could I use Terraform jsondecode() function to convert the json to the map of objects as given below

variable "Services" {
  type = map(object({
   custid     = string
   fname    = string
   lname = string
   }))
  default = {
   "service1" = {
     custid = "1104",
     fname  = "ton",
     lname  = "hatf",
   },
   "service2" = {
     custid = "1105",
     fname  = "ran",
     lname  = "ttt",
  },
   "service3" = {
     custid = "1106",
     fname  = "rin",
     lname  = "wgg",
  }
}
 }

I tried to read it using the below code

  locals {
     json_data = jsondecode(file("${path.module}/Domain.json"))
    }

But unfortunately I don't know how I could use it to build the above map(objects)

mystack
  • 4,910
  • 10
  • 44
  • 75

1 Answers1

4

Your Domain.json is invalid json. The valid json is:

{
   "Domain": {
    "Services": [{
        "service1": [{
            "custid" : "1104",
            "fname" : "ton",
            "lname" : "hatf"
        }],
        "service2": [{
            "custid" : "1105",
            "fname" : "ran",
            "lname" : "ttt"
        }],
        "service3": [{
            "custid" : "1106",
            "fname" : "rin",
            "lname" : "wqg"
        }]
    }]
}
}

Then, your local can be:

locals {
 services = jsondecode(file("${path.module}/Domain.json"))["Domain"]["Services"]
}
Marcin
  • 215,873
  • 14
  • 235
  • 294