hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
42.8k stars 9.56k forks source link

Support comments in JSON/jsondecode #35796

Open skeggse opened 1 month ago

skeggse commented 1 month ago

Terraform Version

Terraform v1.9.0
on darwin_amd64

Use Cases

It's sometimes very useful to define common values for Terraform configuration in standalone data files, such as JSON files. These files can then be consumed from multiple workspaces and used to define values in different places.

For example, we have a JSON file of all the VPC interface endpoints a particular part of our infrastructure depends on, along with detail describing variances for specific endpoints. This feeds into both the endpoint definitions, and some routing configuration in a different workspace.

Attempted Solutions

locals {
  data = jsondecode(
    // Remove comments from the JSON file, because Terraform doesn't support comments in JSON
    // files. Note that this doesn't handle comments on lines with actual JSON, because that would
    // require proper interpretation of JSON strings.
    replace(
      file("${path.module}/vpc_interfaces.json"),
      "/\n\\s*\\/(\\*[\\s\\S]*\\*\\/|\\/.*)/", ""
    )
  )
}

Proposal

Either always allow comments via jsondecode, or add a jsoncdecode function or equivalent.

References

No response

crw commented 1 month ago

Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the đź‘Ť reaction on the issue description to upvote this issue. We also welcome additional use case descriptions.

Traditionally JSON does not support comments, and as far as I can tell the implementation in go-cty also does not support comments. Probably, this would need to be an upstream issue as well, although the maintainers can take care of that if they decide to implement this. Thanks!

bschaatsbergen commented 1 day ago

Hey @skeggse,

Thank you for raising this issue. The intended behavior of Terraform’s jsondecode function is that it expects a valid JSON string, and JSON itself does not support comments.

Supporting comments in JSON would require a much more complex parser to handle them properly, which would introduce significant overhead and complexity that doesn’t seem justified by the benefits. As a workaround, it’s best to preprocess your JSON to remove comments before passing it to jsondecode, or consider using another format like JSON5, which supports comments, before converting it back to valid JSON for jsondecode to process.

Note that you now have the ability to develop your own functions using provider-defined functions. For example, you could develop a tojson function in a JSON5 provider, which would allow you to handle JSON5 files (with comments) in your Terraform configurations.

If you’re interested, please also see the provider-defined functions documentation to learn how to implement functions in your providers. If you’re new to provider development, you can learn how to create a new provider with the Terraform Plugin Framework. For any questions, please visit the Terraform Plugin Development category in our official forum.

To implement full comment support in JSON within Terraform, we would need to introduce custom parsing logic in go-cty, which would be a substantial change, likely introducing more complexity than the problem warrants.

I hope this explanation clarifies the complexity involved, and given the impact, I don’t foresee any immediate development on this. Thank you for raising the issue, and please don’t hesitate to reach out if you have any further questions or ideas.