{"record":{"id":"e5d030c7a6223123","repo":"siyuan-note/siyuan","slug":"generated-tree-s-is-not-valid-json","errorCode":null,"errorMessage":"generated tree [%s] is not valid JSON","messagePattern":"generated tree \\[(.+?)\\] is not valid JSON","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/import_obsidian.go","lineNumber":2372,"sourceCode":"\ttree.Root.SetIALAttr(\"title\", doc.Title)\n\ttree.Root.SetIALAttr(\"updated\", util.TimeFromID(doc.ID))\n\ttree.Root.RemoveIALAttrsByPrefix(\"custom-\")\n}\n\nfunc writeObsidianTempTree(docsTemp string, tree *parse.Tree) error {\n\tif tree == nil || tree.Root == nil {\n\t\treturn errors.New(\"cannot stage an empty tree\")\n\t}\n\tif tree.Root.FirstChild == nil {\n\t\ttree.Root.AppendChild(treenode.NewParagraph(\"\"))\n\t}\n\ttreenode.UpgradeSpec(tree)\n\ttree.Root.SetIALAttr(\"type\", \"doc\")\n\tluteEngine := util.NewLute()\n\trenderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions)\n\tdata := renderer.Render()\n\tif !json.Valid(data) {\n\t\treturn fmt.Errorf(\"generated tree [%s] is not valid JSON\", tree.HPath)\n\t}\n\tif !util.UseSingleLineSave {\n\t\tvar buffer bytes.Buffer\n\t\tif err := json.Indent(&buffer, data, \"\", \"\\t\"); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdata = buffer.Bytes()\n\t}\n\ttarget := filepath.Join(docsTemp, filepath.FromSlash(strings.TrimPrefix(tree.Path, \"/\")))\n\tif err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {\n\t\treturn err\n\t}\n\treturn filelock.WriteFile(target, data)\n}\n\nfunc copyStableObsidianFile(source *obsidianSourceFile, destination string) error {\n\tif err := validateObsidianSourceMetadata(source); err != nil {\n\t\treturn err","sourceCodeStart":2354,"sourceCodeEnd":2390,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/import_obsidian.go#L2354-L2390","documentation":"After rendering the staged tree to JSON via NewJSONRenderer, writeObsidianTempTree verifies the output with `json.Valid`. If the renderer produced invalid JSON, staging fails with this error naming the tree's HPath. It guards against writing a corrupt .sy document file.","triggerScenarios":"Calling `writeObsidianTempTree` when `renderer.Render()` returns bytes that fail `json.Valid(data)`.","commonSituations":"A Lute/renderer bug emitting malformed JSON for unusual inline structures; corrupted tree content after normalization; version mismatch between renderer and JSON schema expectations.","solutions":["Re-run the import for the failing document to see if it reproduces deterministically","Inspect the note at tree.HPath for structures that break JSON rendering (rare inline spans) and simplify them","Update the kernel/Lute and retry; if reproducible, report the document as a renderer bug"],"exampleFix":"// before: hard fail\ndata := renderer.Render()\nif !json.Valid(data) {\n    return fmt.Errorf(\"generated tree [%s] is not valid JSON\", tree.HPath)\n}\n// after: log and skip the bad document instead of aborting the whole import\ndata := renderer.Render()\nif !json.Valid(data) {\n    stats.AddSkipped(\"invalid JSON for \" + tree.HPath)\n    return nil\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := writeObsidianTempTree(docsTemp, tree); err != nil {\n    if strings.Contains(err.Error(), \"is not valid JSON\") {\n        log.Printf(\"renderer bug for %s, skipping\", tree.HPath)\n        return nil\n    }\n    return err\n}","preventionTips":["Keep Lute and renderer versions aligned","Test imports on documents with unusual inline spans","Report reproducible invalid-JSON documents upstream"],"tags":["json","renderer","obsidian-import"],"backgroundTag":"json-parse-error","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}