Append data to JSON in Go
Tags: go, json, tutoral
Published: 2020-01-12 18:00:48

Working on some projects, I needed to write continuous data to JSON files in Go. So as I found various tutorial but none really gave me what I wanted, here is mine!

# Get the files ready

The last thing you want is panics on missing files, so you need to check if the file exists first (here is the [example](https://golangcode.com/check-if-a-file-exists/) I used). I tweaked it a bit more so I could create the file if it didn't exist.

```go
func checkFile(filename string) error {
    _, err := os.Stat(filename)
        if os.IsNotExist(err) {
            _, err := os.Create(filename)
                if err != nil {
                    return err
                }
        }
        return nil
}

```

I know we could return the file itself, but I didn't really needed it in this case.

# Save the Marshall Ryan

I am not entirely sure I understand marshalling completely, I have to confess. What I know is that the Go handle is ready the bytes in an array and you need to send it to a receiving structure to be able to have it human-readable.

And of course the same way back to write to it.

```go
type MyStruct struct {
    StructData string `json:"StructData"`
}

func main() {
    filename := "myFile.json"
    err := checkFile(filename)
    if err != nil {
        logrus.Error(err)
    }

    file, err := ioutil.ReadFile(filename)
    if err != nil {
        logrus.Error(err)
    }

    data := []MyStruct{}

    // Here the magic happens!
    json.Unmarshal(file, &data)
}

```

Now we can work and build more structures to our JSON file.

```go

// MyStruct is an example structure for this program.
type MyStruct struct {
    StructData string `json:"StructData"`
}

func main() {

    [...]
    newStruct := &MyStruct{
        StructData: "peanut",
    }

    data = append(data, *newStruct)

    // Preparing the data to be marshalled and written.
    dataBytes, err := json.Marshall(data)
    if err != nil {
        logrus.Error(err)
    }

    err = ioutil.WriteFile(filename, dataBytes, 0644)
    if err != nil {
        logrus.Error(err)
    }
}

```

That does the work! What are your techniques to write to JSON file and append new data to it?

Here is the whole code:

```go
package main

import (
	"encoding/json"
	"io/ioutil"
	"os"

	"github.com/sirupsen/logrus"
)

// MyStruct is an example structure for this program.
type MyStruct struct {
	StructData string `json:"StructData"`
}

func main() {
	filename := "myFile.json"
	err := checkFile(filename)
	if err != nil {
		logrus.Error(err)
	}

	file, err := ioutil.ReadFile(filename)
	if err != nil {
		logrus.Error(err)
	}

	data := []MyStruct{}

	// Here the magic happens!
	json.Unmarshal(file, &data)

	newStruct := &MyStruct{
		StructData: "peanut",
	}

	data = append(data, *newStruct)

	// Preparing the data to be marshalled and written.
	dataBytes, err := json.Marshal(data)
	if err != nil {
		logrus.Error(err)
	}

	err = ioutil.WriteFile(filename, dataBytes, 0644)
	if err != nil {
		logrus.Error(err)
	}
}

func checkFile(filename string) error {
	_, err := os.Stat(filename)
	if os.IsNotExist(err) {
		_, err := os.Create(filename)
		if err != nil {
			return err
		}
	}
	return nil
}


```