golang how to unmarshal a json object containing a string array

Viewed 36

I have a golang struct that is store as text in database after marshalling to json

type PaymentText struct {
Identifier         string   `json:"identifier,omitempty"`
IdentifiersList    []string `json:"identifiers_list,omitempty"`}


paymentTextJSON, _ := json.Marshal(paymentText)
paymentTextString := string(paymentTextJSON)
#this is stored in db and fetched back to unmarshal to struct again

but when I unmarshal the json string back to struct with a string array field it throws an error

json: cannot unmarshal string into Go value of type app.PaymentText

I think when the string array is converted to text by string(paymentTextJSON) it is in the form of ["a","b"] and unmarshal does not know how to convert it back to []string?

paymentTextJSON, _ := json.Marshal(PaymentTextReadFromDB)

sample json

    {
    "identifier":            "ttttt",
    "identifiers_list":       ["aaaa", "bbb", "ccccc"]
}
0 Answers
Related