[Solved] json: invalid use of string struct tag [closed]


As the error says, your use of ,string is invalid for your JSON input. Remove it:

    Name string `json:"name,omitempty"`

,string can be valid in a JSON tag, and it indicates that the number should be marshaled as a string literal. In the case of a value which is already a string, that means it expects a JSON-quoted string (which your input apparently is not).

This is explained in the docs:

The “string” option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types. This extra level of encoding is sometimes used when communicating with JavaScript programs:

Int64String int64 `json:",string"`

See in the playground for more details.


Also, as @Adrian has pointed out, string: is a meaningless tag (for the purpose of JSON (un)marshaling, anyway). You probably want json: instead of string: (although some library may use a tag called string:… ?

1

solved json: invalid use of string struct tag [closed]