Skip to content

json: cannot unmarshal array into Go struct field Attribute.attributes.values of type structpb.Value #1019

@blakecschmidt

Description

@blakecschmidt

What version of protobuf and what language are you using?
Protobuf version 1.3.2
Go version 1.13.5

What did you do?
I am working with the Google My Business API and I am attempting to retrieve the JSON response of a location.
https://developers.google.com/my-business/reference/rest/v4/accounts.locations/get

After retrieving the JSON, I am unmarshaling it as a Location object. This Location object contains an attribute field which is an array of Attribute objects. Within the attribute object is another array of protobuf Values.

In my Go code, I have a .go file containing all of the structs which make up this Location struct. In this file I am importing the following:

import (
	structpb "github.com/golang/protobuf/ptypes/struct"
)

And when I define this Attribute struct I define the Values with the following:

type Attribute struct {
	AttributeId string `json:"attributeId"`
	ValueType AttributeValueType `json:"valueType"`
	Values []structpb.Value `json:"values"`
	RepeatedEnumValue RepeatedEnumAttributeValue `json:"repeatedEnumValue"`
	UrlValues []UrlAttributeValue `json:"urlValues"`
}

The JSON that I am receiving from the GMB API contains the following attributes component:

"attributes": [
    {
      "attributeId": "pay_debit_card",
      "valueType": "BOOL",
      "values": [
        true
      ]
    },
    {
      "attributeId": "has_wheelchair_accessible_elevator",
      "valueType": "BOOL",
      "values": [
        true
      ]
    },
    {
      "attributeId": "has_service_installation",
      "valueType": "BOOL",
      "values": [
        true
      ]
    },
    {
      "attributeId": "pay_check",
      "valueType": "BOOL",
      "values": [
        true
      ]
    },
    {
      "attributeId": "has_delivery_same_day",
      "valueType": "BOOL",
      "values": [
        true
      ]
    },
    {
      "attributeId": "has_gift_wrapping",
      "valueType": "BOOL",
      "values": [
        false
      ]
    },
    {
      "attributeId": "requires_cash_only",
      "valueType": "BOOL",
      "values": [
        false
      ]
    },
    {
      "attributeId": "has_wheelchair_accessible_entrance",
      "valueType": "BOOL",
      "values": [
        true
      ]
    },
    {
      "attributeId": "has_service_repair",
      "valueType": "BOOL",
      "values": [
        true
      ]
    },
    {
      "attributeId": "wi_fi",
      "valueType": "ENUM",
      "values": [
        "free_wi_fi"
      ]
    },
    {
      "attributeId": "pay_credit_card_types_accepted",
      "valueType": "REPEATED_ENUM",
      "repeatedEnumValue": {
        "setValues": [
          "american_express",
          "discover",
          "mastercard",
          "visa"
        ]
      }

The function in which I am receiving this JSON and unmarshaling is:

func GetStore(accessToken string) {
	response, err := http.Get(fmt.Sprintf("%vaccounts/%v/locations/%v?access_token=%v", gmbBaseUri, "104256381367782693902", "10772730936382702757", accessToken))

	if err != nil {
		fmt.Printf("The HTTP request failed with error %s\n", err)
	}
	data, _ := ioutil.ReadAll(response.Body)
	fmt.Println(string(data))
	var location Location
	if err := json.Unmarshal(data, &location); err != nil {
		panic(err)
	}
	fmt.Println(location.Name)
}

What did you expect to see?
If this code was working correctly, my function would correctly unmarshal the JSON into a Location object and it would print out the name of the Location as specified in the JSON.

What did you see instead?
Instead, I am receiving the following error:

panic: json: cannot unmarshal bool into Go struct field Attribute.attributes.values of type structpb.Value

Which fails at the panic(err) line when unmarshaling.

Anything else we should know about your project / environment?
I am running this locally using the GoLand IDE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions