Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read double-quoted string values using terraform csvdecode?

I have a CSV file that contains the following content:

id,value
123,{"M":{"name_1":{"S":"value_1"}, "name_2":{"S":"value_2"}}}

I’m trying to read this CSV file and create records in DynamoDB the following way:

locals {
  custom_data = csvdecode(file("${path.module}/../custom_data.csv"))
}

resource "aws_dynamodb_table_item" "custom_table_item" {
  for_each = {for row in local.custom_data : row.id => row}

  table_name = aws_dynamodb_table.custom_table.name
  hash_key   = aws_dynamodb_table.custom_table.hash_key

  item = jsonencode({
    "id" : { "S" : each.value.id },
    "value" : jsondecode(each.value.value)
  })

  lifecycle {
    ignore_changes = [item]
  }
}

However, this code doesn’t work and I can’t find any example on how to read the double-quoted values from the CSV file in a way that jsondecode can create the appropriate JSON structure.
Does anyone know how to do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Sanitize your csv file:

id,value
123,"{""M"":{""name_1"":{""S"":""value_1""}, ""name_2"":{""S"":""value_2""}}}"

then:

$ terraform-repl
> csvdecode(file("${path.module}/custom_data.csv"))
tolist([
  {
    "id" = "123"
    "value" = "{\"M\":{\"name_1\":{\"S\":\"value_1\"}, \"name_2\":{\"S\":\"value_2\"}}}"
  },
])

> jsondecode(csvdecode(file("${path.module}/custom_data.csv"))[0].value)
{
  "M" = {
    "name_1" = {
      "S" = "value_1"
    }
    "name_2" = {
      "S" = "value_2"
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading