0

My JSON looks like this:

{ "website": { "id": 8, "account_id": 9, "name": "max", "website_url": "", "subscription_status": "trial", "created_at": "2016-01-24T01:43:41.693Z", "updated_at": "2016-02-21T01:17:53.129Z", } } 

My Website struct looks like this:

type Website struct { Id int64 `json:"id"` AccountId int64 `json:"account_id"` Name string `json:"name"` WebsiteUrl string `json:"website_url"` SubscriptionStatus string `json:"subscription_status"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } 

I then have a Company struct like:

type Company struct { Website Website api *API } 

So in my API client code I have this:

res, status, err := api.request(endpoint, "GET", nil, nil) if err != nil { return nil, err } if status != 200 { return nil, fmt.Errorf("Status returned: %d", status) } r := map[string]Company{} err = json.NewDecoder(res).Decode(&r) fmt.Printf("things are: %v\n\n", r) 

The output I am getting is this:

things are: map[website:{{0 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC} <nil>}] 

How can I debug this, I am not sure why it is not setting the Website struct with correct values.

Update

I added this, I don't see any output:

if err != nil { fmt.Printf("error is not nillllllll!!!:") fmt.Println(err) return nil, err } 
2
  • check the error from Decode. Commented Apr 17, 2016 at 2:18
  • @hobbs I updated my question with the error code, I don't see any output. Commented Apr 17, 2016 at 2:27

1 Answer 1

2

Its because you have too many levels of nesting with the map > Company > Website. The map effectively builds another layer on top of it so its expecting three levels of JavaScript objects nested. In actuality, the JSON you provided only has two with Company > Website.

So there are a couple of ways to get it to work; choose the one that works best for you.

You can do:

r := map[string]Website{} json.NewDecoder(res).Decode(&r) 

Or you can do:

r := Company{} json.NewDecoder(res).Decode(&r) 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! btw, any links to a production example of a API client that I should look at?
No problem! I tend to just use the http.Client and add another layer of abstraction if I need something more domain specific. Not sure if that helps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.