Jeffail/gabs

Generate Array of Struct

Closed this issue · 2 comments

Hi,

is it possible to generate such a JSON structure with gabs?

[{"path":"1","value":1},{"path":"2","value":"test"}]

I have already tested many variations and have not been able to get it right.

Hi @schwinn, thanks for raising this issue! I think the code below is what you're looking for. Please let me know if it helps you:

jsonObj := gabs.Container{}
obj := gabs.New()
// Note that Set returns the underlying created object (namely "1") so don't overwrite obj with it
obj.Set("1", "path")
obj.Set("1", "value")
jsonObj.ArrayAppend(obj)
obj = gabs.New()
obj.Set("2", "path")
obj.Set("test", "value")
jsonObj.ArrayAppend(obj)
fmt.Println(jsonObj.String())

Perfect! Thanks Mihai :-)