argoproj/argo-workflows · error
cannot do template replacements with invalid JSON
Error message
cannot do template replacements with invalid JSON
What it means
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.
Source
Thrown at util/template/replace.go:43
}
// ToAnyMap widens a string map for use with Replace. Callers that track absent optionals
// (skipped/omitted node outputs) build a map[string]any directly so nil entries survive.
func ToAnyMap(m map[string]string) map[string]any {
anyMap := make(map[string]any, len(m))
for k, v := range m {
anyMap[k] = v
}
return anyMap
}
// Replace takes a json-formatted string and performs variable replacement. Values are raw: a nil
// entry marks an absent optional (a skipped/omitted node's output with no default), and a simple
// tag resolving to nil is a terminal error regardless of allowUnresolved, which only governs tags
// whose key is missing entirely.
func Replace(ctx context.Context, s string, replaceMap map[string]any, allowUnresolved bool) (string, error) {
if !json.Valid([]byte(s)) {
return "", errors.New("cannot do template replacements with invalid JSON")
}
t, err := NewTemplate(s)
if err != nil {
return "", err
}
replacedString, err := t.Replace(ctx, replaceMap, allowUnresolved)
if err != nil {
return s, err
}
if !json.Valid([]byte(replacedString)) {
return s, errors.New("cannot finish template replacement because the result was invalid JSON")
}
return replacedString, nil
}
View on GitHub (pinned to 35bff19146)
Solutions
- Fix the JSON syntax of the parameter/template value before substitution
- Ensure any escaped quotes in the template survive marshalling
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at util/template/replace.go:43 when the library encounters an invalid state.
Common situations: See trigger scenarios.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
AI-assisted analysis of argoproj/argo-workflows@35bff19146 (2026-09-03).
Data as JSON: /api/errors/a5bf410486810e63.
Report an issue: GitHub.