How to parse JSON data without struct in Golang

In using Golang, we sometimes need to parse a JSON data without knowing or specifying a concrete type struct. We can do this by converting (json.Unmarshal) JSON data into an interface{}. We can then use it like a map. Accessing it like for example m["username"].(string)

func AddUserHandler(w http.ResponseWriter, r *http.Request) {
    log.Println("Processing Register User")
    var payload interface{} // The interface where we will save the converted JSON data.
    buffer, err := ioutil.ReadAll(r.Body)
    if err != nil {
    r.Body.Close() // Close this
    json.Unmarshal(buffer, &payload) // Convert JSON data into interface{} type
    m := payload.(map[string]interface{}) // To use the converted data we will need to convert it 
                                          // into a map[string]interface{}

    validationErrors := make([]string, 0)
    if len(strings.TrimSpace(m["username"].(string))) == 0 {  // You can now access the data like this m["username"]
        validationErrors = append(validationErrors, "Username is required")

    if len(strings.TrimSpace(m["password"].(string))) == 0 {
        validationErrors = append(validationErrors, "Password is required")

    if len(strings.TrimSpace(m["name"].(string))) == 0 {
        validationErrors = append(validationErrors, "Name is required")

    if len(validationErrors) > 0 {
        respondWithJSON(w, models.ResponsePayloadModel{
            Total:  0,
            Result: nil,
            Errors: validationErrors,

    // Your other codes goes here...