Write a JSON file -- you can do so in VSC (create a file xxx.json) -- containing data about 3 Bi-Co students (fictional is preferred). The file should contain at least the following information for each student:
jsonFile, err := os.Open("users.json") if err != nil { fmt.Println(err) } fmt.Println("Successfully Opened users.json") byteValue, _ := ioutil.ReadAll(jsonFile) jsonFile.Close()My experience with opening files is that if you use the the "run" button in VSC, you need to give the full, absolute path to the file being opened. If you use a terminal window and type "go run xx.go" then you can use a relative path name. The simplest thing to do is to is to simply unmarshal the byte array into a slice of maps. To do so create two types
type jm map[string]interface{} type js []jmthen after the read funcion add the following lines:
var res js json.Unmarshal(byteValue, &res)Done. You have read in the JSON file. Add a print statement to verify that you have succeeded.
Create structs as needed to read your student data file. You may want to update the representation in your JSON to make this easier. Then adjust your code (create, probably two structs, etc) to read the JSON into a slice of structs.
One important thing when you are creating structs for unmarshalling. The variable names inside the structs MUST have an initial capital letter. Why?
data, _ := json.Marshal(students) fmt.Println(string(data))The resulting JSON should be fairly similar to your student file.