0

I'm calling a remote API and getting a JSON response back. I'm trying to convert the *http.Response into a struct that I defined. Everything i've tried so far has resulted in an empty struct. Here is my attempt with json.Unmarshal

type Summary struct {
   Created  string  `json:"created"`          
   High     float64 `json:"high"`             
   Low      float64 `json:"low"`              
}

func getSummary() {

   url := "http://myurl"

   resp, err := http.Get(url)
   if err != nil {
       log.Fatalln(err)
   }

   body, err2 := ioutil.ReadAll(resp.Body)
   if err2 != nil {
       panic(err.Error())
   }

   log.Printf("body = %v", string(body))
   //outputs: {"success":true,"message":"","result":["High":0.43600000,"Low":0.43003737],"Created":"2017-06-25T03:06:46.83"}]}

   var summary = new(Summary)
   err3 := json.Unmarshal(body, &summary)
   if err3 != nil {
       fmt.Println("whoops:", err3)
       //outputs: whoops: <nil> 
   }

   log.Printf("s = %v", summary)
   //outputs: s = &{{0 0 0  0 0 0  0 0 0  0}}


}

What am I doing wrong? The JSON tags in my struct match the json keys from the response exactly...

edit: here is the JSON returned from the API

{
  "success": true,
  "message": "''",
  "result": [
    {
      "High": 0.0135,
      "Low": 0.012,
      "Created": "2014-02-13T00:00:00"
    }
  ]
}

edit i changed the struct to this but still not working

type Summary struct {
   Result struct {
       Created string  `json:"created"`
       High    float64 `json:"high"`
       Low     float64 `json:"low"`
   } 
 }
8
  • yea the program prints the whoops error. Using %v outputs the exact same Commented Mar 23, 2019 at 4:13
  • I added an update. Why is the output of err3 = <nil> if the previous if statement requires that eer3 is not nil? Commented Mar 23, 2019 at 4:22
  • body is not valid JSON. You have to remove the %!(EXTRA string= and the ) in order to be able to use json.Unmarshal on it. Commented Mar 23, 2019 at 4:31
  • Additionally Summary is not the same shape as the JSON data anyway. You need to make it match the shape of the whole JSON object (after removing the portions mentioned above) in order to be able to unmarshal it. Commented Mar 23, 2019 at 4:33
  • 1
    please add the response JSON in the question. Commented Mar 23, 2019 at 4:58

2 Answers 2

2

Change your structure like this

type Summary struct {
  Sucess bool `json:"success"`
  Message string `json:"message"`
  Result []Result `json:"result"`
}

type Result struct {
   Created string  `json:"Created"`
   High    float64 `json:"High"`
   Low     float64 `json:"Low"`
} 

Try this link

Sign up to request clarification or add additional context in comments.

Comments

2

Its because you're trying to unmarshal an array into struct, Use array instead of the Result struct

type Summary struct {
    Result []struct {
        Created string  `json:"created"`
        High    float64 `json:"high"`
        Low     float64 `json:"low"`
    }
}

Use this weblink to convert your JSON objects to Go Struct >> https://mholt.github.io/json-to-go/

1 Comment

That json to go converter is an awesome resource, thanks for sharing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.