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)
}