Your code displays invalid JSON text.
You should also write the field names, and for security quotes, the field names and string values , but do not contain the bool value (otherwise the Go json package will not untie it in bool , for example). Also enclose your values ββin brackets {} as follows:
b.Write([]byte(`{"name":"`)) // started with { b.Write([]byte(i.name)) b.Write([]byte(`","flag":`))
Conclusion (try the full app on Go Playground ):
[{"name":"foo","flag":true},{"name":"bar","flag":false}]
But since you are not doing anything special during the marshal, just export the fields (by starting them with uppercase letters), and the json package will automatically marshal him / unmarshal:
type Info struct { Name string Flag bool }
Try this version on Go Playground .
Exit (pay attention to the names with the upper location "Name" and "Flag" ):
[{"name":"foo","flag":true},{"name":"bar","flag":false}]
You can also use tags if you want to use different names in the JSON text as follows:
type Info struct { Name string `json:"name"` Flag bool `json:"flag"` }
This again leads to a release with names with a lower oval:
[{"name":"foo","flag":true},{"name":"bar","flag":false}]
Read the json.Marshal() function documentation to find out what other parameters and settings you can use with structure tags.