Create a nested JSON from an array of N length

I’ve been working to create a nested JSON from an Array but can’t seem to figure out how to do it. I currently have the following code, however its not working and cannot seem to fix it no matter what I do.
The array that i’m currently working with look as follows. Note i’m trying to make JSON work no matter the array’s length.

    [{2017-11-20 13:18:12 -0600 CST 70.261 2 1} {2017-11-20 13:11:15 -0600 CST 70.253 0 1} {2017-11-20 13:08:45 -0600 CST 70.43 0 1} {2017-11-20 13:05:29 -0600 CST 70.32000000000001 0 1} {2017-11-13 15:32:43 -0600 CST 76.354 0 1} {2017-11-13 15:26:41 -0600 CST 86.273 2 1} {2017-11-13 15:22:59 -0600 CST 86.273 2 1}][{2017-11-20 13:18:12 -0600 CST 70.261}]

The output i would like to would look something like this :

 {
     "Status_message": "successful",
    "Weight_data": [{
    "Weight_date": "2017-11-17 15:22:59 -0600 CST",
    "Weight_kg": 86.273
 }, {
    "Weight_date": "2017-11-14 15:22:59 -0600 CST",
    "Weight_kg": 85.273
 }, {
    "Weight_date": "2017-11-12 15:22:59 -0600 CST",
    "Weight_kg": 76.273
 }, {
    "Weight_date": "2017-11-16 15:22:59 -0600 CST",
     "Weight_kg": 66.273
 }]

My current code looks as follows, two structs

type AutoGenerated struct {
     StatusMessage string `json:"Status_message"`
     Weight_Data   [] Weight_Data
  }

 type Weight_Data [] struct {
      Weight_Date string  `json:"Weight_date"`
     Weight_Kgs   float64 `json:"Weight_kg"`
  }


func main() {


  var mainStruct AutoGenerated



var current_weight [] Weight_Datas

for i, _ := range m.ParseData().Weights {

	current_weight = Weight_Datas{m.ParseData().Weights[i].Date.String(),m.ParseData().Weights[i].Kgs}

	mainStruct.Weight_Datas = append(mainStruct.Weight_Datas, current_weight)


}

final := AutoGenerated{"Success",current_weight}

js, err := json.Marshal(final)


}

Hi & welcome to the community! You have some confusion in the types and variables in your sample that I’ve corrected. You want something like this for your types:

type AutoGenerated struct {
	StatusMessage string       `json:"Status_message"`
	Weights       []WeightData `json:"Weight_data"`
}

type WeightData struct {
	Date time.Time `json:"Weight_date"`
	Kgs  float64   `json:"Weight_kg"`
}

In the top level struct, you have a status message which is string and the weights which is a slice of WeightData (we don’t use snake_case in Go, though you’re welcome to use it in the JSON representation of course).

The WeightData type should be a simple struct and not a slice by itself, when used like this. So I removed the brackets in your type declaration (previously type Weight_Data [] struct ...). The Date attribute is better of as an actual time.Time, the JSON encoder/decoder will handle the formatting here.

(The “AutoGenerated” one should of course have a different name, but I’m leaving that.)

In the main program, we just declare an instance of the top level struct, and then loop to add weights to it. You had two of each kind, which is unnecessary and didn’t really match up type wise.

	mainStruct := AutoGenerated{StatusMessage: "Success"}

	for i := 0; i < 5; i++ {
		w := WeightData{time.Now(), rand.Float64() * 25}
		mainStruct.Weights = append(mainStruct.Weights, w)
	}

	js, _ := json.MarshalIndent(mainStruct, "", "  ")
	fmt.Printf("%s\n", js)

(I used random data instead of the data that you had coming from somewhere else.) The result is about what you want:

{
  "Status_message": "Success",
  "Weight_data": [
    {
      "Weight_date": "2009-11-10T23:00:00Z",
      "Weight_kg": 15.116507199490488
    },
    {
      "Weight_date": "2009-11-10T23:00:00Z",
      "Weight_kg": 23.512727201125312
    },
...

Runnable code:

https://play.golang.org/p/J1bjhX2F20T

4 Likes

Not you have two answers: the one by @calmh, and another on Stack Overflow.

Which of these does help you more?

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