代码之家  ›  专栏  ›  技术社区  ›  mystack

将Json转换为地形图(objects())

  •  0
  • mystack  · 技术社区  · 4 年前

    我有一个json文件,如下所示

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

    我可以使用Terraform jsondecode()函数将json转换为如下所示的对象映射吗

    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",
      }
    }
     }
    

    我试着用下面的代码读它

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

    但不幸的是,我不知道如何使用它来构建上述地图(对象)

    0 回复  |  直到 4 年前
        1
  •  1
  •   Marcin    4 年前

    你的 Domain.json 无效的json .有效的json是:

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

    然后,你的 local 可以是:

    locals {
     services = jsondecode(file("${path.module}/Domain.json"))["Domain"]["Services"]
    }