2

I am trying to create a JSON file with Golang. I have little knowledge of JSON Files and creating them but I have created a program that creates them. In this program, it takes form data from a website and then puts the data into a JSON struct, then adds the information into a folder. I have the 2 sections of data here. I put a comment where the error occurs along with the error

{
    "cl":"[v1]",
    "gr":"[A]",
    "cr":"[8]"
} // End Of File Expected
{
    "cl":"[v2]",
    "gr":"[Z]",
    "cr":"[8]"
}

So my questions are (1) What does the error mean, and (2) How/can I fix this when creating a JSON file with Golang? I can supply the Golang if needed.

4
  • 6
    That’s not valid JSON. If you have a group of items you likely need them in an array, with each element separated by a comma. Commented Dec 20, 2017 at 3:28
  • Could you show me an example of what it should look like? I have looked all over the web but I am quite confused. @Joe Commented Dec 20, 2017 at 3:30
  • 2
    [ { “cl”:”[v1]”, ... }, { “cl”:”[v2]”, ... } ] Commented Dec 20, 2017 at 3:38
  • By stream do you mean a bunch of values that keep being added? Then I intend to have a stream of values, I am storing User Data. Commented Dec 20, 2017 at 4:25

1 Answer 1

4

So other than the json not being formatted correctly, here is an example of how to create json using a struct and json struct tags.

Proper JSON

[ {key:value, key value}, {key:value, key value} ]

What you have is {key:value, key value} {key:value, key value}

Which is two separate objects instead of one object in an array.

If you are reading this from file and the data is returned like your example then you might have to split on the newline to separate each object and unmarshal them separately.

Otherwise the below should server as an example.

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "strconv"
)

type j struct {
    Cl []string `json:"cl"`
    Gr []string `json:"gr"`
    Cr []string `json:"cr"`
}

func main() {
    // create an instance of j as a slice
    var data []j
    // using a for loop for create dummy data fast
    for i := 0; i < 5; i++ {
        v := strconv.Itoa(i)
        data = append(data, j{
            Cl: []string{"foo " + v},
            Gr: []string{"bar " + v},
            Cr: []string{"foobar " + v},
        })
    }

    // printing out json neatly to demonstrate
    b, _ := json.MarshalIndent(data, "", " ")
    fmt.Println(string(b))

    // writing json to file

    _ = ioutil.WriteFile("file.json", b, 0644)

    // to append to a file
    // create the file if it doesn't exists with O_CREATE, Set the file up for read write, add the append flag and set the permission
    f, err := os.OpenFile("/var/log/debug-web.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0660)
    if err != nil {
        log.Fatal(err)
    }
    // write to file, f.Write()
    f.Write(b)

    // if you are doing alot of I/O work you may not want to write out to file often instead load up a bytes.Buffer and write to file when you are done... assuming you don't run out of memory when loading to bytes.Buffer


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

6 Comments

How would you append to the file then? I tried unmarshaling it, adding data, then marshaling it again, but it just appends a whole new data set.
I'll add an example in a bit
I'm also curious about how you would append the data to the file. :)
@GoofBall101 my answer has been updated to append. Now if the object loading into the file is an array of json objects, what would have to be done is slightly different. You'd have to open the file, unmarshal the bytes until the proper struct. Append to that struct and override that file with the marshalled contents.
Thank you. I think I had it, but I was not overriding the file. I just kept appending to the file.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.