Jsondecode produces no output and no error

Hello

I’m experiencing similar issues trying to use the

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

When I run the below code, it do not output any information and no error.

provider "azurerm" {

    features{}

}

locals {

      resource_groupname = csvdecode(file("./ResourceTypes.csv"))

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

    }

    resource "azurerm_resource_group" "Main" {

      count    = length(local.resource_groupname)

      name     =  local.resource_groupname[count.index].resourcetype

      location = "North europe"

    } 

     resource "azurerm_resource_group" "json" {

       # (other settings)

       name = "NewMetricAlert"

       location = "North europe"

Kindly help out

Hi @ighobulu,

I don’t see any references to local.json_data in the configuration you shared, but it’s also missing a closing } so I wonder if you accidentally shared only part of your configuration. Can you share the part of the configuration where you tried to make use of local.json_data too, and explain what your goals were for that? Thanks!

locals {

      resource_groupname = csvdecode(file("./ResourceTypes.csv"))

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

    }

    resource "azurerm_resource_group" "Main" {

      count    =  length(local.resource_groupname)

      name     =  local.resource_groupname[count.index].resourcetype

      location = "North europe"

    } 

     resource "azurerm_resource_group" "json" {

       # (other settings)

       name = local.json_data

       location = "North europe"

     }

     

      output "local_json_data" {

        value = local.json_data

      }

Above is the complete code and thank you for the quick response.

my end goal is trying to get a Json file and decode it to save as a .TF file, the json file is an azure monitor file.

kindly get the monitor file on the above link

Hi @ighobulu,

In the configuration you shared I see the following:

resource "azurerm_resource_group" "json" {
  name = local.json_data
}

I find this a little confusing because the Monitor.json you shared is an object rather than a string. Perhaps you meant to say name = local.json_data.name to access the name property from the JSON document.

Can you also include the full output you saw when you ran terraform plan? I don’t really understand what you meant by “no information and no error”: do you mean that Terraform literally printed nothing at all? If it printed any messages then it would help if you share them so that I can understand how Terraform is interpreting your configuration.

Error: Incorrect attribute value type
│
│   on Monitor.tf line 18, in resource "azurerm_resource_group" "json":
│   18:        name = local.json_data
│     ├────────────────
│     │ local.json_data is object with 9 attributes
│
│ Inappropriate value for attribute "name": string required.

Above is the output of the code

Hi @ighobulu,

That error message seems to confirm what I mentioned in my previous comment: local.json_data is an object resulting from the JSON decoding, and so it’s not a suitable value for name. But if you intended to use the name property from your JSON object then you could write local.json_data.name to access that attribute specifically, which would then be of the correct type (a string).

provider "azurerm" {

    features{}

}

locals {

      resource_groupname = csvdecode(file("./ResourceTypes.csv"))

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

    }

    resource "azurerm_resource_group" "Main" {

      count    =  length(local.resource_groupname)

      name     =  local.resource_groupname[count.index].resourcetype

      location = "North europe"

    } 

     resource "azurerm_resource_group" "json" {

       # (other settings)

       name = local.json_data.name

       location = "North europe"

     }

      output "local_json_data" {

        value = local.json_data.name

      }

when I use the name property from the JSON object as shown in the above code, its returns an error.

╷
│ Error: "name" may only contain alphanumeric characters, dash, underscores, parentheses and periods
│
│   with azurerm_resource_group.json,
│   on Monitor.tf line 15, in resource "azurerm_resource_group" "json":
│   15:      resource "azurerm_resource_group" "json" {

Hello @apparentlymart

I have been able to clear the error now, I can see the content of the file when I run this code.

provider "azurerm" {

    features{}

}

locals {

      resource_groupname = csvdecode(file("./ResourceTypes.csv"))

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

    }

    resource "azurerm_resource_group" "Main" {

      count    =  length(local.resource_groupname)

      name     =  local.resource_groupname[count.index].resourcetype

      location = "North europe"

    } 

     resource "azurerm_resource_group" "name" {

       # (other settings)

       name = local.json_data.name

       location = "North europe"

     }

      output "local_json_data" {

        value = local.json_data

      }

is there any way I can attach the attributes of the JSON file to a variable in terraform? EG. i have the “name” attribute in the JSON file. Then I want to hold that the value, attached to the “name” attribute, and assigned it to a variable in terraform.

Hello @apparentlymart

Is there any assistance from you?