d2lang/d2 · error
decode diagram hash JSON: got closing token %v, want }
Error message
decode diagram hash JSON: got closing token %v, want }
What it means
While walking nested JSON arrays during stable hashing, each object must be closed with '}'. If the decoder returns a different token where the object close is expected, this error reports the offending token. It means the JSON structure is not a well-formed object at that position.
Source
Thrown at d2target/d2target.go:304
stable, err := stableHashURL(raw)
if err != nil {
return err
}
valueEnd := int(dec.InputOffset())
if valueEnd < valueStart || valueEnd > len(b) {
return fmt.Errorf("decode icon URL for stable diagram hash: invalid offsets %d:%d", valueStart, valueEnd)
}
out.Write(b[lastWrite:valueStart])
out.Write(stable)
lastWrite = valueEnd
}
closeToken, err := dec.Token()
if err != nil {
return err
}
if closeToken != json.Delim('}') {
return fmt.Errorf("decode diagram hash JSON: got closing token %v, want }", closeToken)
}
case '[':
for dec.More() {
if err := walkValue(); err != nil {
return err
}
}
closeToken, err := dec.Token()
if err != nil {
return err
}
if closeToken != json.Delim(']') {
return fmt.Errorf("decode diagram hash JSON: got closing token %v, want ]", closeToken)
}
default:
return fmt.Errorf("decode diagram hash JSON: unexpected delimiter %q", delim)
}
return nilView on GitHub (pinned to 0d69dca6f5)
Solutions
- Validate the JSON with json.Valid before hashing
- Re-serialize the diagram from the compiled d2target.Diagram rather than editing serialized JSON
- Fix the generator/editor that produced mismatched brackets
Example fix
// before
b = []byte(`{"icon": ["url"]`) // missing }
// after
b = []byte(`{"icon": ["url"]}`) Defensive patterns
Strategy: validation
Validate before calling
if !json.Valid(b) {
return errors.New("diagram JSON is malformed")
} Prevention
- Never patch serialized JSON with string/regex edits
- Validate JSON before hashing
- Regenerate from the source diagram instead of editing output
When it happens
Trigger: Stable-hashing JSON with mismatched brackets — e.g. an object terminated by ']' instead of '}' — usually from corrupted, truncated, or hand-edited diagram JSON.
Common situations: Manual string surgery on diagram JSON; partial reads from cache files; regex-based JSON rewriting that broke bracket pairing.
Related errors
- decode diagram hash JSON: got closing token %v, want ]
- decode diagram hash JSON: unexpected delimiter %q
- decode diagram hash JSON: object key has type %T
- decode icon URL for stable diagram hash: %w
- decode icon URL for stable diagram hash: invalid offsets %d:
AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31).
Data as JSON: /api/errors/39112584ed505df4.
Report an issue: GitHub.