{"record":{"id":"8057c5ee03b8b3b0","repo":"siyuan-note/siyuan","slug":"invalid-json-s","errorCode":null,"errorMessage":"invalid JSON: %s","messagePattern":"invalid JSON: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/cli/cmd/database.go","lineNumber":322,"sourceCode":"\t\tfmt.Println(\"ok\")\n\t\treturn nil\n\t},\n}\n\nvar databaseItemUpdateCmd = &cobra.Command{\n\tUse:   \"update --av <avID> --key <keyID> --item <itemID> --value <json>\",\n\tShort: \"Update a cell value\",\n\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\tavID, _ := cmd.Flags().GetString(\"av\")\n\t\tkeyID, _ := cmd.Flags().GetString(\"key\")\n\t\titemID, _ := cmd.Flags().GetString(\"item\")\n\t\tvalueStr, _ := cmd.Flags().GetString(\"value\")\n\t\tif avID == \"\" || keyID == \"\" || itemID == \"\" || valueStr == \"\" {\n\t\t\treturn fmt.Errorf(\"--av, --key, --item and --value are required\")\n\t\t}\n\t\tvar valueData map[string]any\n\t\tif err := json.Unmarshal([]byte(valueStr), &valueData); err != nil {\n\t\t\treturn fmt.Errorf(\"invalid JSON: %s\", err)\n\t\t}\n\n\t\tif dryRun {\n\t\t\tfmt.Printf(\"[dry-run] Would update cell in database %s: key=%s item=%s\\n\", avID, keyID, itemID)\n\t\t\treturn nil\n\t\t}\n\n\t\tif _, err := model.UpdateAttributeViewCell(nil, avID, keyID, itemID, valueData); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tmodel.AppendPushReloadAttrViewEntry(avID)\n\t\tfmt.Println(\"ok\")\n\t\treturn nil\n\t},\n}\n\nfunc printUnusedItems(items []*model.UnusedItem) {\n\tif len(items) == 0 {","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/cli/cmd/database.go#L304-L340","documentation":"After the four required flags pass, `database update` unmarshals the --value string into a `map[string]any` with encoding/json. If the payload is not syntactically valid JSON, or is valid JSON but not a JSON object (e.g. an array, bare string, or number), json.Unmarshal returns a SyntaxError or UnmarshalTypeError which is wrapped and returned. The error message embeds the underlying json error verbatim, which usually pinpoints the offending byte offset.","triggerScenarios":"Passing --value with unbalanced braces/quotes; passing a JSON array `[1,2]` or a bare scalar `\"hello\"` or `42`; passing a shell-quoted string whose inner quotes were stripped by the shell; passing JSON with trailing commas or single quotes; passing the path to a JSON file instead of its contents.","commonSituations":"Forgetting to single-quote the JSON in the shell so double quotes get consumed; building the JSON with naive string concatenation that produces `{'text': 'x'}` (single quotes are invalid JSON); piping a file path instead of `$(cat file.json)`; locale/encoding issues injecting smart quotes; assuming the value accepts a raw string instead of an object.","solutions":["Validate the JSON locally first: `echo '<json>' | jq .` — jq exits non-zero on malformed JSON and reports the position.","Single-quote the whole payload in the shell so inner double quotes survive: `--value '{\"col\":\"v\"}'`.","Ensure the top-level JSON value is an object `{}`, not an array or scalar, because the target type is map[string]any.","If building JSON programmatically, use jq -c or a JSON serializer rather than string concatenation; load file contents via `--value \"$(cat payload.json)\"`.","Read the offset in the embedded json error (e.g. `invalid character 'x' looking for beginning of value`) and fix that exact byte."],"exampleFix":"// before\nsiyuan database update --av a --key k --item i --value {text: hello}\n// after\nsiyuan database update --av a --key k --item i --value '{\"text\":\"hello\"}'","handlingStrategy":"validation","validationCode":"# validate JSON and that it is an object before invoking\nprintf '%s' \"$VALUE\" | jq -e . >/dev/null && [ \"$(printf '%s' \"$VALUE\" | jq -r 'type')\" = 'object' ] || { echo '--value must be a JSON object' >&2; exit 2; }\nsiyuan database update --av \"$AV\" --key \"$KEY\" --item \"$ITEM\" --value \"$VALUE\"","typeGuard":"// Go: ensure the string parses to a JSON object before unmarshalling\nfunc isJSONObject(s string) bool {\n\tvar m map[string]any\n\treturn json.Unmarshal([]byte(s), &m) == nil\n}","tryCatchPattern":"# show the embedded json error offset to the user\nif ! siyuan database update --av \"$AV\" --key \"$KEY\" --item \"$ITEM\" --value \"$VALUE\" 2>err.txt; then\n  grep -q 'invalid JSON' err.txt && echo \"fix JSON payload (run: echo \\\"$VALUE\\\" | jq .)\" >&2\n  exit 1\nfi","preventionTips":["Always single-quote JSON payloads in the shell.","Build JSON with jq -c or a serializer, never string concatenation.","For file-based payloads, pass \"$(cat file.json)\" and lint the file with jq first."],"tags":["cli","cobra","json","validation","attribute-view","go"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}