Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug (or at least misleading error message) in jsondecode function when there is duplicate keys in given json string #36351

Open
erwan-simon opened this issue Jan 16, 2025 · 2 comments
Labels
bug cty Use in conjunction with "upstream" when cty is the relevant upstream new new issue not yet triaged upstream

Comments

@erwan-simon
Copy link

Terraform Version

v1.10.4

Terraform Configuration Files

Does not work :

locals {
  test = jsondecode("{ \"hello\": { \"test\": true }, \"hello\": { \"test2\": false } }")
}

Works :

locals {
  test = jsondecode("{ \"hello\": { \"test\": true }, \"something_else_than_hello\": { \"test2\": false } }")
}

Debug Output

During plan or apply :

╷
│ Error: Error in function call
│
│   on terraform.tf line 2, in locals:
│    2:   test = jsondecode("{ \"hello\": { \"test\": true }, \"hello\": { \"test2\": false } }")
│     ├────────────────
│     │ while calling jsondecode(str)
│
│ Call to function "jsondecode" failed: unsupported attribute
│ "test".

Expected Behavior

The dupplicate key should overwrite the previous ones in the map, or at least the error message should state why is "test" unsupported.

Actual Behavior

It fails saying that "test" (the dupplicate key) is an unsupported attribute

Steps to Reproduce

  1. terraform init
  2. terraform plan

Additional Context

No response

References

No response

Generative AI / LLM assisted development?

No response

@erwan-simon erwan-simon added bug new new issue not yet triaged labels Jan 16, 2025
@crw crw changed the title Bug (or at least misleading error message) in jsondecode function when there is dupplicate keys in given json string Bug (or at least misleading error message) in jsondecode function when there is duplicate keys in given json string Jan 16, 2025
@crw
Copy link
Contributor

crw commented Jan 16, 2025

Thanks for this issue report. I would imagine this would come from the upstream library provided by golang, it might be worth investigating whether there is an upstream bug for this issue. Thanks!

@jbardin jbardin added upstream cty Use in conjunction with "upstream" when cty is the relevant upstream labels Jan 17, 2025
@jbardin
Copy link
Member

jbardin commented Jan 17, 2025

Hi @erwan-simon,

Yes, the error is coming from an upstream library, though not the Go standard library, but rather the go-cty library used to handle the underlying type system in Terraform. The problem stems from Terraform (cty) being mostly statically typed, and trying to decode multiple values of different types into a single object key.

Because duplicate keys are conventionally tolerated in json decoding rather than failing, the decoding library attempts to override the first object encountered with the second, but it cannot decode the second object due to it having a different type -- {test=string} is distinct from {test2=string}.

That explains why the error is what it is, but you're right that is not a helpful error. We'll have to see if the upstream library can refactor the decoding a bit to give a more useful message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cty Use in conjunction with "upstream" when cty is the relevant upstream new new issue not yet triaged upstream
Projects
None yet
Development

No branches or pull requests

3 participants