jsongounmarshalling

JSON Unmarshal struct case-sensitively


Is there any way to make json.Unmarshal not accept a case-insensitive match? I receive a JSON with tags such as "e" and "E" and would like to unmarshal the object with tag "e" but ignore the one with "E". Right now the only solution I found was to define a struct containing both tags and then to simply ignore tag "E", but I'm looking for a cleaner solution.

From the official doc:

To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match.


Solution

  • Unfortunately this is not something currently supported by the standard json library.

    According to https://golang.org/pkg/encoding/json/#Unmarshal

    Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match

    There is no way to turn this behaviour off.