Terraform: jsonencode - Invalid value for "val" parameter: argument must not be null.

Created on 11 Oct 2019  ·  7Comments  ·  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.10

Terraform Configuration Files

✗ terraform console
Acquiring state lock. This may take a few moments...
> jsonencode(null)

>
Error: Invalid function argument

  on <console-input> line 1:
  (source code not available)

Invalid value for "val" parameter: argument must not be null.

Debug Output

Expected Behavior

Documentation states that jsonencode(null) will output a null value.
https://www.terraform.io/docs/configuration/functions/jsonencode.html

Additional info

It appears there's no test for jsonencode with a null value. https://github.com/hashicorp/terraform/blob/master/config/hcl2_shim_util_test.go

bug config good first issue v0.12

All 7 comments

I do not think that the doc implies that you can pass null as parameter to jsonencode().
The way that I understand it is that it will map the Terraform value Null to the null json type :

> jsonencode({test: null})
{"test": null}

But I found a strange behaviour with the v0.12.16-dev version with jsondecode() which does not deserialize json null value to Terraform null value :

> jsondecode("{\"test\": null}")
{}

In this case the doc for jsondecode clearly states that a null json value is converted to the Terraform language null value, so it's probably a bug.

It looks like jsondecode is an alias for JSONDecodeFunc from the go-cty library.
I think the change would have to be made here.

hi there! 👋the behaviour described by @antonin-arquey seems to still be happening on the latest release v0.12.18 and there don't appear to be any open PRs for it. Assuming it's actually a beginner-friendly issue I'll have a little look this weekend??

I also ran into this issue on v0.12.20
I was working on creating a template, for me the following was a suitable workaround for null strings

some_var = var.some_var == null ? format("%v", var.some_var) : jsonencode(var.some_var)

More info can be found here on string formatting
https://www.terraform.io/docs/configuration/functions/format.html

It looks like this change: https://github.com/hashicorp/terraform/commit/c51d22bdebd9cd85dee7eaae450f0f6f068e674a introduced the jsondecode behavior described by @antonin-arquey.

Any idea if having nils in ConfigValue maps is still an issue? I can put a PR to fix the decode behavior if otherwise.

Hello I would like to contribute to this issue as a first timer is that possible ?!

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings