2

There is such workflow of writing json objects to file :

for {
    Step 1. create json object
    Step 2. Save object to file
}

So I wrote such code

f, _ := os.Create("output.json")
defer f.Close()
a1 := A{Name:"John", Surname:"Black"}
a2 := A{Name:"Mary", Surname:"Brown"}

a1_json, _ := json.MarshalIndent(a1, "", "\t")
a2_json, _ := json.MarshalIndent(a2, "", "\t")
f.Write(a1_json)
f.Write(a2_json)

As a result I have:

{
    "Name": "John",
    "Surname": "Black"
}{
    "Name": "Mary",
    "Surname": "Brown"
}

Which is not correct json file, because it doesn't have opening and closing braces and comma like this:

[
  {
    "Name": "John",
    "Surname": "Black"
  },
  {
    "Name": "Mary",
    "Surname": "Brown"
  }
]

How can I write to file in an appropriate way?

2
  • @Volker Maybe there are some go libraries which can handle such cases? Commented Mar 6, 2018 at 13:52
  • Then SO is the wrong place to look for such. But honestly: writing a , to a file doesn't require a dedicated package. The overhead to import something like this is bigger than just writing the ,. Commented Mar 6, 2018 at 15:24

2 Answers 2

3

Just make a slice of the structures and save it. This will create the JSON array.

f, _ := os.Create("output.json")
defer f.Close()
as := []A{
    {Name:"John", Surname:"Black"},
    {Name:"Mary", Surname:"Brown"},
}
as_json, _ := json.MarshalIndent(as, "", "\t")
f.Write(as_json)

If you really want you can separate the elements manually.

f, _ := os.Create("output.json")
defer f.Close()
a1 := A{Name:"John", Surname:"Black"}
a2 := A{Name:"Mary", Surname:"Brown"}

f.Write([]byte("[\n"))
a1_json, _ := json.MarshalIndent(a1, "", "\t")
f.Write([]byte(",\n"))
a2_json, _ := json.MarshalIndent(a2, "", "\t")
f.Write([]byte("]\n"))

f.Write(a1_json)
f.Write(a2_json)

You can also consider using JSON Streaming which can achieve your goals but the syntax is slightly different.

Sign up to request clarification or add additional context in comments.

4 Comments

it breaks a workflow, because I want to get an json object and then store it to file in a loop.
if that is the case, you would need to write each one in different file
This is the correct way of using the Go's JSON library. If you want, you can add the brackets and commas yourself to the stream but you do it at your own risk. It is not hard to do it but it will not stand a code review.
@RomaKarageorgievich I added a not on JSON Streaming, you may find it useful.
1

Put those structs into a slice and marshall the slice instead

f, err := os.Create("output.json")
if err != nil{
   panic(err)
}
defer f.Close()
a1 := A{Name:"John", Surname:"Black"}
a2 := A{Name:"Mary", Surname:"Brown"}

a := []A{a1, a2}
a_json, err := json.MarshalIndent(a, "", "\t")
if err != nil{
   panic(err)
}
f.Write(a_json)

Also, always check for err wherever possible

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.