xpzouying/xiaohongshu-mcp · error

failed to unmarshal notes: %w

Error message

failed to unmarshal notes: %w

What it means

extractUserProfileData 已拿到 notes JSON,但 json.Unmarshal 到 notesData(notes 二维数组/index/query)失败:笔记数据结构与结构体定义不匹配或 JSON 非法。

Source

Thrown at xiaohongshu/user_profile.go:114

		return nil, fmt.Errorf("user.notes.value not found in __INITIAL_STATE__")
	}

	// 解析用户信息
	var userPageData struct {
		Interactions []UserInteractions `json:"interactions"`
		BasicInfo    UserBasicInfo      `json:"basicInfo"`
	}
	if err := json.Unmarshal([]byte(userDataResult), &userPageData); err != nil {
		return nil, fmt.Errorf("failed to unmarshal userPageData: %w", err)
	}

	var notesData struct {
		Notes [][]Feed `json:"notes"`
		Index int      `json:"index"`
		Query string   `json:"query"`
	}
	if err := json.Unmarshal([]byte(notesResult), &notesData); err != nil {
		return nil, fmt.Errorf("failed to unmarshal notes: %w", err)
	}

	// tab 不符时报错,避免把别的 tab 的内容当成结果返回
	want := tab
	if want == "" {
		want = TabNotes
	}
	if notesData.Query != "" && ProfileTab(notesData.Query) != want {
		return nil, fmt.Errorf("当前 tab 为 %q,与请求的 %q 不符", notesData.Query, want)
	}

	// 组装响应
	response := &UserProfileResponse{
		UserBasicInfo: userPageData.BasicInfo,
		Interactions:  userPageData.Interactions,
	}

	// 每个 tab 的内容存在各自的下标里,只取当前 tab 的,避免混入其他 tab

View on GitHub (pinned to 332d196854)

Solutions

  1. 按底层 %w 错误定位不兼容字段,升级库版本适配新 Feed 结构
  2. 检查 notesResult 是否含 null 元素或一维结构
  3. 若仅个别笔记触发,可尝试缩小返回范围或过滤后再解析
  4. 向维护者报告结构差异

Example fix

// before
res, err := client.UserProfile(ctx, userID, "note")
// after
if err != nil && strings.Contains(err.Error(), "unmarshal notes") {
    log.Printf("Feed schema 可能已变更: %v", err)
    return handleSchemaChange(err)
}
Defensive patterns

Strategy: type-guard

Validate before calling

if notesResult == "" || notesResult == "null" {
    return errors.New("notes 数据为空")
}

Type guard

func looksLikeNotes(raw string) bool {
    var probe struct {
        Notes [][]json.RawMessage `json:"notes"`
    }
    return json.Unmarshal([]byte(raw), &probe) == nil && len(probe.Notes) > 0
}

Try / catch

res, err := client.UserProfile(ctx, userID, tab)
if err != nil && strings.Contains(err.Error(), "unmarshal notes") {
    var base error
    errors.As(err, &base) // 取底层 json 错误定位不兼容字段
    return handleFeedSchemaChange(err)
}

Prevention

When it happens

Trigger: 调用 UserProfile / GetMyProfileViaSidebar 时,__INITIAL_STATE__.user.notes.value 的 JSON 结构与 [][]Feed 不符——例如 Feed 内字段类型变化(id 从数字变字符串)、notes 变为一维数组、包含 null 项。

Common situations: 小红书改版调整了 Feed 结构;笔记列表含特殊卡片(广告/置顶)字段类型不同;activeTab 序列化后出现 null 嵌套导致数组元素非法。

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/139f240a357c44f7. Report an issue: GitHub.