Nested json in golang

Hi All,

I am trying to print out an nested json with my go microservices. I have queried DB and collected the response. I want to print details of each ID as nested json. Below are the details.

My code:

type rresponse struct {
	Set_ID   []int64         `json:"set_id"`
	Data_Set successresponse `json:"datasets"`
}

type successresponse struct {
	LTST_VER_IND []string `json:"tst_ver_ind"`
	DATA_TS      []string `json:"data_ts"`
	CREATN_TS    []string `json:"creatn_ts"`
}
 
 
 dataset := successresponse{
     			LTST_VER_IND: dataVal.LTST_VER_IND, //dataVal is object for DB respnse.
     			DATA_TS:      dataVal.DATA_TS,
     			CREATN_TS:    dataVal.CREATN_TS,
     		}
     response := &rresponse{
     		      Set_ID:   dataVal.SET_ID,
     			Data_Set: dataset,
     }
     rsp.WriteEntity(response)
     jsonData, err := json.MarshalIndent(response, "", "  ")
     if err != nil {
            log.Println(err)
     }

Below is the output:

   {
        "set_id": [
            28,
            29
        ],
        "datasets": {
            "tst_ver_ind": [
                "Y",
                "Y"
            ],
            "data_ts": [
                "{\"String\":\"\",\"Valid\":false}",
                "{\"String\":\"\",\"Valid\":false}"
            ],
            "creatn_ts": [
                "2020-04-03T20:37:41Z",
                "2020-04-03T21:11:54Z"
            ]
        }
    }

The Output i want:

{
	"set_id":[
		28 {
			"tst_ver_ind": "Y",
			"data_ts": "{\"String\":\"\",\"Valid\":false}",
			"creatn_ts": "2020-04-03T20:37:41Z",
		},
		29 {
			"tst_ver_ind": "Y",
			"data_ts": "{\"String\":\"\",\"Valid\":false}",
			"creatn_ts": "2020-04-03T21:11:54Z",
		},
	],
}

Can you guys help to to modify my code to achieve this?

Show us the input.

Above is my DB rows. I am fetching only column 1, 6,7,8. What i am trying to print on json is, json structure to be as below.

{
    set_id1 [
         col1: value1,
         col2: value2,
         col3: value3,
     ],
    set_id2 [
         col1: value1,
         col2: value2,
         col3: value3,
     ],
   set_id3 [
         col1: value1,
         col2: value2,
         col3: value3,
     ]
}

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