diff --git a/cty/function/stdlib/json.go b/cty/function/stdlib/json.go index 07901c65..02770a65 100644 --- a/cty/function/stdlib/json.go +++ b/cty/function/stdlib/json.go @@ -12,6 +12,7 @@ var JSONEncodeFunc = function.New(&function.Spec{ Name: "val", Type: cty.DynamicPseudoType, AllowDynamicType: true, + AllowNull: true, }, }, Type: function.StaticReturnType(cty.String), @@ -24,6 +25,10 @@ var JSONEncodeFunc = function.New(&function.Spec{ return cty.UnknownVal(retType), nil } + if val.IsNull() { + return cty.StringVal("null"), nil + } + buf, err := json.Marshal(val, val.Type()) if err != nil { return cty.NilVal, err diff --git a/cty/function/stdlib/json_test.go b/cty/function/stdlib/json_test.go index e67ba6e0..5c1348fc 100644 --- a/cty/function/stdlib/json_test.go +++ b/cty/function/stdlib/json_test.go @@ -52,6 +52,10 @@ func TestJSONEncode(t *testing.T) { cty.DynamicVal, cty.UnknownVal(cty.String), }, + { + cty.NullVal(cty.String), + cty.StringVal("null"), + }, } for _, test := range tests {