I have a problem while writting json data to my json file. I do it in incorrect format and I don’t know how to fix it.
Here’s the incorrect format:
{"todo":{"done":false,"task":"test3"}} {"todo":{"done":false,"task":"test3"}} {"todo":{"done":false,"task":"test2"}}
Here’s the code for it
`func WriteJSONFile(value string) {
filename := "todobudData.json"
data := map[string]interface{}{
"todo": map[string]interface{}{
"done": false,
"task": value,
},
}
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("sad boi")
return
}
fmt.Println(string(jsonData))
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.SetFlags(log.Ldate | log.Lshortfile)
log.Println(err)
}
f.WriteString(string(jsonData))
f.Close()
}
`
I don’t know how to write json file with specific format. I would like, later to retrive individual tasks form the json file, and I dont have any idea how to fix its format.
I tried to convert the map to the var and to wrap object in json array but it didn’t work
New contributor
1