googleapis/mcp-toolbox · error

unable to unmarshal JSON: %w

Error message

unable to unmarshal JSON: %w

What it means

In processRows (internal/sources/spanner/spanner.go:157), after successfully reading a string column value, the code runs json.Unmarshal on it to produce a map[string]any for 'object_details'. If the string is not valid JSON (malformed syntax, empty string, truncated payload), the unmarshal fails and this wrapped error is returned. The underlying json.UnmarshalSyntaxError/TypeError is included via %w.

Source

Thrown at internal/sources/spanner/spanner.go:157

		if err != nil {
			return nil, fmt.Errorf("unable to parse row: %w", err)
		}

		rowMap := orderedmap.Row{}
		cols := row.ColumnNames()
		for i, c := range cols {
			if c == "object_details" { // for list graphs or list tables
				val := row.ColumnValue(i)
				if val == nil { // ColumnValue returns the Cloud Spanner Value of column i, or nil for invalid column.
					rowMap.Add(c, nil)
				} else {
					jsonString, ok := val.AsInterface().(string)
					if !ok {
						return nil, fmt.Errorf("column 'object_details' is not a string, but %T", val.AsInterface())
					}
					var details map[string]any
					if err := json.Unmarshal([]byte(jsonString), &details); err != nil {
						return nil, fmt.Errorf("unable to unmarshal JSON: %w", err)
					}
					rowMap.Add(c, details)
				}
			} else {
				rowMap.Add(c, row.ColumnValue(i))
			}
		}
		out = append(out, rowMap)
	}
	return out, nil
}

func (s *Source) RunSQL(ctx context.Context, readOnly bool, statement string, params map[string]any) (any, error) {
	var results []any
	var err error
	var opErr error
	stmt := spanner.Statement{
		SQL: statement,

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Validate the stored data: SELECT the column and run it through a JSON linter to find the malformed rows
  2. Fix the producer writing the column so it serializes valid JSON objects
  3. If arrays/other shapes are expected, change the unmarshal target from map[string]any to json.RawMessage or a matching type
  4. Clean up or repair corrupted rows with an UPDATE or re-run the ETL job
Defensive patterns

Strategy: validation

Validate before calling

func isValidJSONObject(s string) bool {
	var m map[string]any
	return json.Unmarshal([]byte(s), &m) == nil
}

Prevention

When it happens

Trigger: A Spanner STRING column that the code expects to contain a JSON object contains invalid JSON: malformed braces, an empty string, plain text, or a JSON array instead of an object.

Common situations: Application code wrote non-JSON text into a JSON-designated column; truncated writes; column contains 'null' scalar or a JSON array where the code expects map[string]any; data corruption from a bad ETL job.

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 googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/91c4dc1877d5d06a. Report an issue: GitHub.