I'm reading a number of JSON files from S3, and want to return them all as one large JSON array. I have a struct matching my JSON data, and a for loop iterating over all objects in my s3 bucket. Each time I read, I unmarshal to my struct array. I want to append to my struct array so that I can get all the JSON data rather than just one file's data. Is there anyway to do this in Golang?
Yes, you should create a temporary array to Unmarshal the contents of each JSON, then append the items to your final result array in order to return the whole collection as one item.
See here an example of doing that.
In your case input
would come from each of the S3 files you mention. Also, you would probably put that unmarshal logic in its own function to be able to call it for each input JSON.
package main
import (
"encoding/json"
"fmt"
"log"
)
type Record struct {
Author string `json:"author"`
Title string `json:"title"`
}
func main() {
var allRecords []Record
input := []byte(`[{
"author": "Nirvana",
"title": "Smells like teen spirit"
}, {
"author": "The Beatles",
"title": "Help"
}]`)
var tmpRecords []Record
err := json.Unmarshal(input, &tmpRecords)
if (err != nil) {
log.Fatal(err)
}
allRecords = append(allRecords, tmpRecords...)
fmt.Println("RECORDS:", allRecords)
}