{"record":{"id":"a5bf410486810e63","repo":"argoproj/argo-workflows","slug":"cannot-do-template-replacements-with-invalid-json","errorCode":null,"errorMessage":"cannot do template replacements with invalid JSON","messagePattern":"cannot do template replacements with invalid JSON","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"util/template/replace.go","lineNumber":43,"sourceCode":"}\n\n// ToAnyMap widens a string map for use with Replace. Callers that track absent optionals\n// (skipped/omitted node outputs) build a map[string]any directly so nil entries survive.\nfunc ToAnyMap(m map[string]string) map[string]any {\n\tanyMap := make(map[string]any, len(m))\n\tfor k, v := range m {\n\t\tanyMap[k] = v\n\t}\n\treturn anyMap\n}\n\n// Replace takes a json-formatted string and performs variable replacement. Values are raw: a nil\n// entry marks an absent optional (a skipped/omitted node's output with no default), and a simple\n// tag resolving to nil is a terminal error regardless of allowUnresolved, which only governs tags\n// whose key is missing entirely.\nfunc Replace(ctx context.Context, s string, replaceMap map[string]any, allowUnresolved bool) (string, error) {\n\tif !json.Valid([]byte(s)) {\n\t\treturn \"\", errors.New(\"cannot do template replacements with invalid JSON\")\n\t}\n\n\tt, err := NewTemplate(s)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treplacedString, err := t.Replace(ctx, replaceMap, allowUnresolved)\n\tif err != nil {\n\t\treturn s, err\n\t}\n\n\tif !json.Valid([]byte(replacedString)) {\n\t\treturn s, errors.New(\"cannot finish template replacement because the result was invalid JSON\")\n\t}\n\n\treturn replacedString, nil\n}\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/argoproj/argo-workflows/blob/35bff19146f5a6ada77468c431f2624bd577e373/util/template/replace.go#L25-L61","documentation":"Replace refuses to do variable substitution because the input string s failed json.Valid before any tags were processed. The template text itself is not valid JSON, so no replacement can be attempted.","triggerScenarios":"Thrown at util/template/replace.go:43 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fix the JSON syntax of the parameter/template value before substitution","Ensure any escaped quotes in the template survive marshalling"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"35bff19146f5a6ada77468c431f2624bd577e373","analyzedAt":"2026-09-03T19:34:35.908Z","contentChangedAt":"2026-09-03T19:34:35.908Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}