How to create nested json like this from database sql?

"categories": [
    {
        "category": [
            {
                "label": "Q1"
            },
            {
                "label": "Q2"
            },
            {
                "label": "Q3"
            },
            {
                "label": "Q4"
            }
        ]
    }
],
"dataset": [
    {
        "seriesname": "Previous Year",
        "data": [
            {
                "value": "10000"
            },
            {
                "value": "11500"
            },
            {
                "value": "12500"
            },
            {
                "value": "15000"
            }
        ]
    },
    {
        "seriesname": "Current Year",
        "data": [
            {
                "value": "25400"
            },
            {
                "value": "29800"
            },
            {
                "value": "21800"
            },
            {
                "value": "26800"
            }
        ]
    }
]

affter execute select query you store in map[string]interface{} then convert to json it s work

You are going to have to provide a lot more info if you want help. Nobody here knows anything about your DB structure, what code you have, etc, so they don’t have anywhere to even start.

You can start by creating a struct that is appropriate for your data. For convenience, you could paste your JSON in https://mholt.github.io/json-to-go/ to get a quick and dirty auto generated Go struct.

Then all you have to do is to fill your struct with values from your database and transform it to JSON using json.Marshal or json.NewEncoder(w).Encode.

Here is an example: https://play.golang.org/p/-EjhzgLkA_B

Thank You, it really help me :slight_smile:

@joncalhoun apologize if my question not complete

    func ApiChartProductionChannelYearbyYear(w http.ResponseWriter,r *http.Request){
  w.Header().Set("Access-Control-Allow-Origin","*")
  w.Header().Set("Content-Type","application-json")
  if r.Method =="POST"{
    type Category struct{
      Label           string `json:"label"`
    }
    type Categories struct{
      Category        interface{}   `json:"category"`
    }
    type Dataset struct{
        Seriesname     int      `json:"seriesname"`
        Value          float64  `json:"value"`
    }
    type ChartList struct{
      Categories      Categories  `json:"categories"`
      Dataset         Dataset     `json:"dataset"`
      Feedback        string      `json:"feedback"`
    }
    var res                = []ChartList{}
    var each               = ChartList{}
    var rescat             = []Category{}
    var eachcat            = Category{}
    var rescats            = []Categories{}
    var eachcats           = Categories{}
    var tokenkey           = strings.Replace(r.FormValue("key"), "'", "\\'", -1)
    var tokensecret        = strings.Replace(r.FormValue("secret"), "'", "\\'", -1)
    var Feedbacks          = config.CheckLoginAPI(tokenkey,tokensecret)
    if Feedbacks != "Y"{
    each.Feedback     = Feedbacks
    res           = append(res,each)
    }else{
    var db = config.ConnectSQLSERVER()
    defer db.Close()
    var sql   = `SELECT segment FROM segment`
    col,errsql      := db.Query(sql)
    if errsql !=nil{
      each.Feedback  = errsql.Error()
      res        = append(res,each)
    }else{
    for col.Next(){
      var errs                = col.Scan(&eachcat.Label)
      if errs!=nil{
        log.Print(errs.Error())
      }
      rescat                 = append(rescat,eachcat)
    }
    eachcats.Category      = rescat
    rescats                = append(rescats,ea)
    each.Feedback          = "Y"
    res                    = append(res,each)
    }
    }
    var result,error = json.Marshal(res)
    if error!=nil{
      http.Error(w,error.Error(),http.StatusInternalServerError)
      return 
 T   }
    w.Write(result)
    return
  }else{
  http.Error(w,config.Strings("errmethod"),http.StatusBadRequest)
  }
}

My FIeld for segment Table

This JSON to create Fusion Chart Json Multi Series

@koka_vamsikrishna could you please give me example?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.