siyuan-note/siyuan · error

invalid JSON

Error message

invalid JSON: %s

What it means

After flag presence validation, `siyuan database item update` unmarshals the `--value` flag into a map[string]any via encoding/json. If the string is not valid JSON, or valid JSON that is not an object, json.Unmarshal errors and RunE wraps it as `invalid JSON: <detail>`. The cell value model expects a JSON object (e.g. {"type":"text","text":"..."}).

Solutions

  1. Quote the JSON properly for your shell and pass a JSON object: `--value '{"type":"text","text":"done"}'`
  2. Validate the JSON with a parser (e.g. `echo "$VALUE" | python -m json.tool`) before invoking the command
  3. On Windows cmd, escape inner double quotes or write the command in a script file to avoid quoting loss
  4. Confirm the payload is a JSON object (map), not an array or scalar, since it unmarshals into map[string]any

Example fix

// before
siyuan database item update --av $AV_ID --key $KEY_ID --item $ROW_ID --value done
// after
siyuan database item update --av $AV_ID --key $KEY_ID --item $ROW_ID --value '{"type":"text","text":"done"}'
Defensive patterns

Strategy: validation

Validate before calling

VALUE='{"type":"text","text":"done"}'
echo "$VALUE" | python3 -m json.tool > /dev/null || { echo "invalid JSON value" >&2; exit 1; }

Prevention

When it happens

Trigger: Passing `--value` with plain text (e.g. `--value hello`), a JSON array or scalar (e.g. `[1,2]` or `"x"`), truncated JSON, or unquoted special characters that the shell mangled (quotes stripped, $ expanded, newlines).

Common situations: Shell quoting issues stripping double quotes from the JSON; writing `--value {type:text}` without quotes; passing a value captured from logs with a trailing message; using single quotes in cmd.exe/PowerShell where they do not preserve the string.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/8057c5ee03b8b3b0. Report an issue: GitHub.

Appendix: source

Thrown at kernel/cli/cmd/database.go:327

		fmt.Println("ok")
		return nil
	},
}

var databaseItemUpdateCmd = &cobra.Command{
	Use:   "update --av <avID> --key <keyID> --item <itemID> --value <json>",
	Short: "Update a cell value",
	RunE: func(cmd *cobra.Command, args []string) error {
		avID, _ := cmd.Flags().GetString("av")
		keyID, _ := cmd.Flags().GetString("key")
		itemID, _ := cmd.Flags().GetString("item")
		valueStr, _ := cmd.Flags().GetString("value")
		if avID == "" || keyID == "" || itemID == "" || valueStr == "" {
			return fmt.Errorf("--av, --key, --item and --value are required")
		}
		var valueData map[string]any
		if err := json.Unmarshal([]byte(valueStr), &valueData); err != nil {
			return fmt.Errorf("invalid JSON: %s", err)
		}

		if dryRun {
			fmt.Printf("[dry-run] Would update cell in database %s: key=%s item=%s\n", avID, keyID, itemID)
			return nil
		}

		if _, err := model.UpdateAttributeViewCell(nil, avID, keyID, itemID, valueData); err != nil {
			return err
		}
		model.AppendPushReloadAttrViewEntry(avID)
		fmt.Println("ok")
		return nil
	},
}

func printUnusedItems(items []*model.UnusedItem) {
	if len(items) == 0 {

View on GitHub (pinned to 9f775e8a12)