gastownhall/beads · error
parsing JSON: %w
Error message
parsing JSON: %w
What it means
After reading the raw formula JSON, formulaToTOML unmarshals it into a generic map to preserve structure. If the file content is not valid JSON, json.Unmarshal fails and the error is wrapped as 'parsing JSON: %w'. This indicates the source file itself is malformed (syntax error, truncation, wrong format), not a conversion logic problem.
Source
Thrown at cmd/bd/formula.go:681
// formulaToTOML converts a Formula to TOML bytes.
// Uses a custom structure optimized for TOML readability.
func formulaToTOML(f *formula.Formula) ([]byte, error) {
// We need to re-read the original JSON to get the raw structure
// because the Formula struct loses some ordering/formatting
if f.Source == "" {
return nil, fmt.Errorf("formula has no source path")
}
// Read the original JSON
jsonData, err := os.ReadFile(f.Source)
if err != nil {
return nil, fmt.Errorf("reading source: %w", err)
}
// Parse into a map to preserve structure
var raw map[string]interface{}
if err := json.Unmarshal(jsonData, &raw); err != nil {
return nil, fmt.Errorf("parsing JSON: %w", err)
}
// Fix float64 to int for known integer fields
fixIntegerFields(raw)
// Encode to TOML
var buf bytes.Buffer
encoder := toml.NewEncoder(&buf)
encoder.Indent = ""
if err := encoder.Encode(raw); err != nil {
return nil, fmt.Errorf("encoding TOML: %w", err)
}
// Post-process to convert escaped \n in strings to multi-line strings
result := convertToMultiLineStrings(buf.String())
return []byte(result), nil
}View on GitHub (pinned to 71377f2769)
Solutions
- Validate the JSON: run it through `jq . file.json` or python -m json.tool to find the syntax error at the reported offset.
- Fix the JSON syntax (missing comma/brace, trailing comma, single quotes).
- Restore the file from git or regenerate it with `bd formula export`/the canonical writer.
- Confirm the file is actually JSON, not TOML or HTML; if it's already TOML, no conversion is needed.
Example fix
// before
{ "name": "deploy", "steps": [ { "cmd": "go test" } ] } // trailing content/truncated
// after
{
"name": "deploy",
"steps": [{ "cmd": "go test" }]
} Defensive patterns
Strategy: validation
Validate before calling
# validate before converting jq empty .beads/formulas/deploy.json || echo "invalid JSON"
Try / catch
if _, err := formulaToTOML(f); err != nil {
var syntaxErr *json.SyntaxError
if errors.As(err, &syntaxErr) {
// report syntaxErr.Offset to locate the malformed byte
}
} Prevention
- Run jq/python json.tool on formula JSON after hand edits.
- Never edit formula JSON without re-validating; prefer `bd formula` commands.
- Restore corrupted files from git rather than patching blindly.
- Ensure downloads are JSON (check Content-Type) before saving as formula sources.
When it happens
Trigger: Running formula conversion against a source JSON file that is corrupted, truncated (partial write), hand-edited with invalid syntax, or not JSON at all (e.g. already TOML, or an HTML error page saved by mistake).
Common situations: Manual edits to formula JSON introducing syntax errors; interrupted writes leaving truncated files; formulas downloaded from a URL where an error page was saved; files converted between formats incorrectly.
Related errors
- parsing formula: %w
- failed to parse gh output: %w
- parse gh output: %w
- failed to parse JSONL line: %w
- failed to parse memory record: %w
AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30).
Data as JSON: /api/errors/62eb3eb10e0588d7.
Report an issue: GitHub.