Open skeggse opened 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!
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.
Terraform Version
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
Proposal
Either always allow comments via
jsondecode
, or add ajsoncdecode
function or equivalent.References
No response