Home > Software engineering >  how to add prefix json string in golang
how to add prefix json string in golang

Time:08-03

I am newbie. I have a image in my db which is stored as a path to it. In api json I have to output this path and add a prefix in front of it, how can I do that? My function:

func GetAllSliders(c *gin.Context) {
    var sliders models.Slider

    config.DB.Model(models.Slider{}).Where("id=?", 1).Update("image", ("https://spares-dt.ru/"   models.Slider{}.Image)) //i tried this, but it doesnt work

    if err := config.DB.Find(&sliders).Error; err != nil {
        c.JSON(http.StatusInternalServerError, err.Error())
    } else {
        c.JSON(http.StatusOK, gin.H{"data": sliders})
    }
}

Json output i have:

{
    "data": {
        "id": 1,
        "main_text": "123",
        "upper_text": "123",
        "down_text": "123",
        "image": "data/photos/sliders/image.PNG"
    }
}

I want:

{
    "data": {
        "id": 1,
        "main_text": "123",
        "upper_text": "123",
        "down_text": "123",
        "image": "https://spakes-dt.ru/data/photos/sliders/image.PNG"
    }
}

And my struct:

type Slider struct {
    Id        uint   `json:"id" gorm:"primaryKey"`
    MainText  string `json:"main_text"`
    UpperText string `json:"upper_text"`
    DownText  string `json:"down_text"`
    Image     string `json:"image"`
}

CodePudding user response:

Simply change the Image field's value before JSON marshaling it:

if err := config.DB.Find(&sliders).Error; err != nil {
    c.JSON(http.StatusInternalServerError, err.Error())
} else {
    sliders.Image = "https://spares-dt.ru/"   sliders.Image
    c.JSON(http.StatusOK, gin.H{"data": sliders})
}

CodePudding user response:

you should use exec

config.DB.Exec(`UPDATE slider SET image = "https://spares-dt.ru/"   iamge WHERE id = ?`, 1)

CodePudding user response:

This is an example of how custom json marshalling works. Modifying your model by adding the MarshalJson method will help you to customize.

package main

import (
    "encoding/json"
    "fmt"
)

type Slider struct {
    ID        int    `json:"id"`
    MainText  string `json:"main_text"`
    UpperText string `json:"upper_text"`
    DownText  string `json:"down_text"`
    Image     string `json:"image"`
}

//Add MarshalJson function
func (u *Slider) MarshalJSON() ([]byte, error) {
    return json.Marshal(&struct {
        ID        int    `json:"id"`
        MainText  string `json:"main_text"`
        UpperText string `json:"upper_text"`
        DownText  string `json:"down_text"`
        Image     string `json:"image"`
    }{
        ID:        u.ID,
        MainText:  u.MainText,
        UpperText: u.UpperText,
        DownText:  u.DownText,
        Image:     "https://spakes-dt.ru/"   u.Image,
    })
}

func main() {
    data := Slider{Image: "data/photos/sliders/image.PNG"}
    bdata, _ := json.Marshal(data)
    fmt.Println(string(bdata))
}

This will produce the following output.

{
  "id": 0,
  "main_text": "",
  "upper_text": "",
  "down_text": "",
  "image": "data/photos/sliders/image.PNG"
}

Update your code something like this after you create MarshalJson method

func GetAllSliders(c *gin.Context) {
    var sliders models.Slider

    config.DB.Model(models.Slider{}).Where("id=?", 1)//i tried this, but it doesnt work

    if err := config.DB.Find(&sliders).Error; err != nil {
        c.JSON(http.StatusInternalServerError, err.Error())
    } else {
        b, _  := json.Marshal(sliders)
        c.JSON(http.StatusOK, gin.H{"data": string(b)})
    }
}
  • Related