googleapis/mcp-toolbox · error

expected []byte for JSON column, but got %T

Error message

expected []byte for JSON column, but got %T

What it means

RunSQL's JSON-column branch found that the scanned value for a JSON-typed column is not []byte as the MySQL driver normally returns — the type assertion val.([]byte) failed, so the JSON cannot be unmarshaled.

Source

Thrown at internal/sources/tidb/tidb.go:159

			return nil, fmt.Errorf("unable to parse row: %w", err)
		}
		vMap := make(map[string]any)
		for i, name := range cols {
			val := rawValues[i]
			if val == nil {
				vMap[name] = nil
				continue
			}

			// mysql driver return []uint8 type for "TEXT", "VARCHAR", and "NVARCHAR"
			// we'll need to cast it back to string
			switch colTypes[i].DatabaseTypeName() {
			case "JSON":
				// unmarshal JSON data before storing to prevent double
				// marshaling
				byteVal, ok := val.([]byte)
				if !ok {
					return nil, fmt.Errorf("expected []byte for JSON column, but got %T", val)
				}
				var unmarshaledData any
				if err := json.Unmarshal(byteVal, &unmarshaledData); err != nil {
					return nil, fmt.Errorf("unable to unmarshal json data %s", val)
				}
				vMap[name] = unmarshaledData
			case "TEXT", "VARCHAR", "NVARCHAR":
				byteVal, ok := val.([]byte)
				if !ok {
					return nil, fmt.Errorf("expected []byte for text-like column, but got %T", val)
				}
				vMap[name] = string(byteVal)
			default:
				vMap[name] = val
			}
		}
		out = append(out, vMap)
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Check the actual Go type reported by %T in the message
  2. Avoid driver/server combos that return JSON columns as other types
  3. Handle or convert the value before the branch
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/sources/tidb/tidb.go:159 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/2c4fab762673a0ea. Report an issue: GitHub.