0

My GoLang struct:

type myPojo struct {
    ID                bson.ObjectId                 `json:"id" bson:"_id,omitempty"`
    Start             time.Time                     `json:"start"`
}

POST API JSON input request:

{
    "Start":ISODate("2013-10-01T00:00:00.000Z")
}

My code to convert input JSON request to Golang Struct:

func myPostApi(w http.ResponseWriter, r *http.Request, db mongoDB) {
    w.Header().Set("Content-Type", "application/json")
    decoder := json.NewDecoder(r.Body)
    var inputObj myPojo
    err := decoder.Decode(&inputObj)

    if err != nil {
        //This gets executed
        log.Println("Error occurred converting POST input json to myPojo data.")
        log.Println(err)
    }
}

Above code is not able to convert, and it goes inside error if block and prints below, please help.

2018/02/25 22:12:44 Error occurred converting POST input json to myPojo data.
2018/02/25 22:12:44 invalid character 'I' looking for beginning of value
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Nikhil Joshi
  • 817
  • 2
  • 12
  • 34
  • What was the error, if you print it out? – Mark Feb 25 '18 at 22:03
  • Hello @Mark, added error "invalid character 'I' looking for beginning of value" in the main question, thanks for looking into it. – Nikhil Joshi Feb 25 '18 at 22:16
  • 1
    That error suggests the request body couldn't be decoded because it's not valid json. Look closely at the request body itself, and check it's valid json, according to the [json specification](https://www.json.org). – Mark Feb 25 '18 at 22:27

1 Answers1

0

The

ISODate("2013...")

value is not valid JSON. That looks like a symbol or function call, neither of which are allowed in JSON. And there is no date type in JSON:

The "right" JSON date format

Jonah Benton
  • 3,598
  • 1
  • 16
  • 27