Home > Mobile >  Go: Unable to unmarshal json value
Go: Unable to unmarshal json value

Time:01-12

I have jsonb value stored in postgres columns which I need to convert to object to send the response

I am unable to convert JSON -> struct using json.Unmarshal method

type Result struct {
    Id          int 
    Expertise   string // because json string 
    Languages   string // because json string 
}

// saving dd query data to result struct 
e, _ := json.Marshal(data.Expertise)
l, _ := json.Marshal(data.Languages)
    row := r.db.QueryRow(`
        INSERT INTO filters (user_id, expertise, languages)
        VALUES ($1, $2, $3)
        ON CONFLICT (user_id) DO 
        UPDATE SET expertise=$2, languages=$3
        RETURNING id, expertise, languages;
    `, userid, e, l)

    var res Result 
    err := row.Scan(&res.Id, &res.Expertise, &res.Languages)

Then I take this the Expertise and Languages field and UNMARSHAL THEM

// helper method 
func Unmarshal(value string) interface{} {
    var obj interface{}
    err := json.Unmarshal([]byte(value), &obj)
    return obj
}

type CreateFilterResponse struct {
    Id        int           `json:"id"`
    Expertise interface{}   `json:"expertise"`
    Languages interface{}   `json:"languages"`
}

response := dto.CreateFilterResponse{
        Id: res.Id,
        Expertise: Unmarshal(res.Expertise), //            
  •  Tags:  
  • Related