#json.Unmarshal giving default values and no error

9 messages · Page 1 of 1 (latest)

iron escarp
winter quartz
#

The struct fields are not exported so they can't be seen by other packages.
Fields must be exported for this to work.

#

Fields must start with an upper case character for them to exported.

#

You need to do it like this:

type ManifestLayer struct {
    MediaType string `json:"mediaType"`
    Size      int64  `json:"size"`
    Digest    string `json:"digest"`
}

type OciManifest struct {
    SchemaVersion int64           `json:"schemaVersion"`
    MediaType     string          `json:"mediaType"`
    Config        ManifestLayer   `json:"config"`
    Layers        []ManifestLayer `json:"layers"`
}
iron escarp
#

Ahhh, cool, thanks!

winter quartz
#

You're welcome. 🙂

#

Those things that I added to the struct are called "struct tags". The encoding/json package look them up at runtime. If they don't exist, it'll use the field's name.

#

So by adding the struct tags, the fields can be exported but the JSON keys will start with lower case.

south baneBOT
#

If your question has been solved, please close the thread with </solved:1022173889255706726>