Manage JSON data using Terraform

Hello

I have a scenario where in I need to get a JSON from a http server and
update some values in JSON and push the JSON file to the provisioned VM.
I am able to get the file using below code.

data "http" "someFile" {
  url = var.Source
}

variable Source {
  default = "https://hostname.com/files/file.json"
}

output result {
  value = data.http.someFile.body
}

I am trying to refer further using data.http.someFile.body.key1 but it fails as Terraform cannot evaluate the key values in the body section. Is there any way to make changes to the returned JSON before I push the file to resource (VM) I create?

Also, Is there a way I can create a local file using template_file option and get the content of JSON into it?

I was able to create a JSON file out of http data output. code as below:

data "template_file" "createjson" {

  template = "${file("${path.module}/scripts/jsonblank.tpl")}"

  vars = {

    body = data.http.someFile.body

  }

}

resource "local_file" "createjson" {

  content  = data.template_file.createjson.rendered

  filename = "${path.module}/scripts/finaljson.json"

}

Trying to figure out to update JSON values based on keys…

Hi @VickyWinner,

The body attribute of the http data source is a string containing the raw data returned from the server, so if you intend to interpret the data inside as JSON then you will need to first decode it using the jsondecode function.

output "result" {
  value = jsondecode(data.http.some_file.body)
}

I’m not sure I understand the rest of what you are asking here, but perhaps if you can try the jsondecode function to solve your first problem then you’ll make more progress and get some more information (new error messages, etc) about your second problem which may then allow me to understand better what your second goal is.