json.Unmarshal returning blank structure

Your struct fields are not exported. This is because they start with a lowercase letter.

EntryCount // <--- Exported
entryCount // <--- Not exported

When I say “not exported”, I mean they are not visible outside of your package. Your package can happily access them because they are scoped locally to it.

As for the encoding/json package though – it cannot see them. You need to make all of your fields visible to the encoding/json package by making them all start with an uppercase letter, thereby exporting them:

type Status struct {
    Status  string
    Node_id string
}

type Meta struct {
    To         string
    From       string
    Id         string
    EntryCount int64
    Size       int64
    Depricated bool
}

type Mydata struct {
    Metadata  Meta
    Status []Status
}

See it working on the Go Playground here

You should also reference the Golang specification for answers. Specifically, the part that talks about Exported Identifiers.

Leave a Comment

tech