Skip to content

associations list: JSON parse error when results exist (toObjectId type mismatch) #51

Description

@jack-tolley

Description

hspt associations list fails with a JSON unmarshal error whenever the association query returns results. Empty result sets return cleanly, making the bug deceptive — it only surfaces when there's actual data.

Steps to reproduce

hspt associations list --from-type contacts --from-id 12345 --to-type notes -o json

Expected

A JSON array of association objects.

Actual

json: cannot unmarshal number into Go struct field Association.results.toObjectId of type string

Root cause

The HubSpot CRM v4 associations API returns toObjectId as a JSON number:

{
  "toObjectId": 98765,
  "associationTypes": [...]
}

But the Go struct in api/associations.go declares it as string:

type Association struct {
	ToObjectID       string            `json:"toObjectId"`
	AssociationTypes []AssociationType `json:"associationTypes"`
}

encoding/json cannot unmarshal a JSON number into a Go string field, so it fails on every non-empty response.

Suggested fix

Change the field type from string to json.Number:

import "encoding/json"

type Association struct {
	ToObjectID       json.Number       `json:"toObjectId"`
	AssociationTypes []AssociationType `json:"associationTypes"`
}

json.Number accepts both JSON numbers and strings, and its .String() method returns the numeric value as a string for display. This is a one-line change with no downstream breakage.

Environment

  • hubspot-cli HEAD on main
  • HubSpot CRM v4 associations API (/crm/v4/objects/{type}/{id}/associations/{toType})
  • Go 1.21+

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions