You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform解析JSON文件报错求助:无法访问字符串类型属性

Terraform解析JSON文件报错问题解决

作为Terraform新手,正在学习相关知识,尝试解析一份示例JSON文件并展示其中的键值对,编写了如下代码但运行失败,请求帮助。

示例JSON文件 (example.json)

{
    "Clients":[
        {
            "alias": "XXX-ED",
            "name": "XXX-ED | XXX Cloud | Dev/Test",
            "prefix": "xxxed",
            "id": 12345,
            "cluster": 3,
            "ZoneName": "East US 2",
            "EnvironmentName": "Dev/Test",
            "AzureSubscriptionID": "xxxxxx-xxxx-xxxxx-xxxx-xxxxxx",
            "AzureDirectory": "xxxxxx.onmicrosoft.com",
            "AzureTenantID": "xxxxxx-xxxx-xxxxx-xxxx-xxxxxx"
        },
        {
            "alias": "XXX-EM",
            "name": "XXX-EM | XXX Cloud | Management",
            "prefix": "xxxem",
            "id": null,
            "cluster": null,
            "ZoneName": "East US 2",
            "EnvironmentName": "Management",
            "AzureSubscriptionID": "xxxxxx-xxxx-xxxxx-xxxx-xxxxxx",
            "AzureDirectory": "xxxxxx.onmicrosoft.com",
            "AzureTenantID": "xxxxxx-xxxx-xxxxx-xxxx-xxxxxx"
        }
    ]
}

编写的Terraform代码

data "local_file" "input"{
  filename = "${path.module}/example.json"
}

locals {
  raw_json_object = jsondecode(data.local_file.input.content)
  root_node       = flatten(local.raw_json_object["Clients"])
  
  expected_output = [
    for clients in local.root_node :[
      for key in clients:
        key.AzureDirectory
      ]
    ]
  }
}

output "raw_content" {
  value = local.expected_output
}

报错信息

Planning failed. Terraform encountered an error while generating this plan. Error: Unsupported attribute on main.tf line 38, in locals: 38: key.AzureDirectory
Can't access attributes on a primitive-typed value (string).


问题分析与解决方案

报错原因

内层循环for key in clients是遍历每个client对象的键名(字符串类型,比如"alias"、"name"),而非client对象本身。字符串是原始类型,无法访问.AzureDirectory属性,因此触发报错。

针对不同需求的修正代码

需求1:提取所有Client的AzureDirectory值

如果目标是获取每个Client的AzureDirectory,直接遍历root_node中的client对象即可:

data "local_file" "input"{
  filename = "${path.module}/example.json"
}

locals {
  raw_json_object = jsondecode(data.local_file.input.content)
  root_node       = flatten(local.raw_json_object["Clients"])
  
  expected_output = [for client in local.root_node : client.AzureDirectory]
}

output "raw_content" {
  value = local.expected_output
}

运行后输出会是:

[
  "xxxxxx.onmicrosoft.com",
  "xxxxxx.onmicrosoft.com"
]

需求2:展示每个Client的所有键值对

如果要完整展示每个Client的所有键值对结构,可调整内层循环遍历键值对:

data "local_file" "input"{
  filename = "${path.module}/example.json"
}

locals {
  raw_json_object = jsondecode(data.local_file.input.content)
  root_node       = flatten(local.raw_json_object["Clients"])
  
  expected_output = [
    for client in local.root_node : {
      for key, value in client : key => value
    }
  ]
}

output "raw_content" {
  value = local.expected_output
}

运行后会输出原始的Client对象列表,保留所有键值对。

需求3:将每个Client的键值对转为列表形式(如[{"key": "alias", "value": "XXX-ED"}, ...])

如果需要这种格式,可修改为:

data "local_file" "input"{
  filename = "${path.module}/example.json"
}

locals {
  raw_json_object = jsondecode(data.local_file.input.content)
  root_node       = flatten(local.raw_json_object["Clients"])
  
  expected_output = [
    for client in local.root_node : [
      for key, value in client : {
        key = key
        value = value
      }
    ]
  ]
}

output "raw_content" {
  value = local.expected_output
}

内容的提问来源于stack exchange,提问作者Suman Ghosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 01:44:57