Dennis
Dennis

Reputation: 77

Struct / Array format append to JSON file

I have a array/struct that constantly printing

for{
        results, errz := client.ReadHoldingRegisters(0, 3)
        if errz != nil {
            fmt.Printf("%v\n", errz)
        }

        fmt.Printf("results %v\n", results)
    }

Printing output will be like this.

[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]

How do i add it into append it into json format? I'm very new to GOLANG. I printed the type out

fmt.Printf("var1 = %T\n", results) 

Results is []uint8 I need save as int on json format.

Upvotes: 0

Views: 637

Answers (2)

Amir Jani
Amir Jani

Reputation: 420

I used to have the same problem, so I fixed it with below code:

// sample-entity.go
type Sample struct {
    sample int `db:"sample" json:"sample"`,
}

writing to json file

// it will create a new file if exists already too
jsonFile, jsonFileError := os.Create(directory + "/[file_name].json")
jsonToWrite := []Sample{
    {
         sample: 1
    }      
}

if jsonFileError != nil {
    panic(jsonFileError)
}
defer jsonFile.Close()

// write in json file
jsonWriter := json.NewEncoder(jsonFile)
jsonWriter.Encode(jsonFile)

Upvotes: 0

brunoff
brunoff

Reputation: 4219

Different approaches to solve to problem.

The simple (and safe) approach:

// import "fmt" "strings"
j := fmt.Sprintf(`{"data":{"1":%s}}`, strings.Join(strings.Fields(fmt.Sprintf("%d", results)), ","))

Previously in the comments I have made this lazier approach, without strings, but it is less safe:

// import "fmt"
j := fmt.Sprintf("%#v",*results); j = "{ \"data\": { \"1\": \""+j[7:len(j)-1]+"\"} }"
// works when the array size is between 1 and 9, above this, the "7" must increase

Now using the native golang json infrastructure.

Here, an example, using hard-coded results := []uint{0, 0, 0, 0, 0, 0}

package main

import (
    "encoding/json"
    "io/ioutil"
    "log"
)
type d struct {
    Data o `json:"data"`
}
type o struct {
    One []uint `json:"1"`
}
func main() {
    results := []uint{0, 0, 0, 0, 0, 0}
    j, _ := json.Marshal(&d{Data:o{One:results}})
    err := ioutil.WriteFile("output.json", []byte(j), 0777) // i can't test here I don't know if []byte(j) or []byte(string(j)) should be used
    if err != nil {
        log.Fatal(err)
    }
}

But once your array is of uint8 instead of uint and golang's json encode []uint8 (which is the same as []byte) as base64 strings, we have to implement our own Marshaller to avoid this behaviour by implementing MarshalJSON, in the same way as seen on How to marshal a byte/uint8 array as json array in Go?.

package main

import (
    "encoding/json"
    "io/ioutil"
    "strings"
    "log"
    "fmt"
)
type d struct {
    Data o `json:"data"`
}
type o struct {
    One []uint8 `json:"1"`
}
func (t *o) MarshalJSON() ([]byte, error) {
    var one string
    if t.One == nil {
        one = "null"
    } else {
        one = strings.Join(strings.Fields(fmt.Sprintf("%d", t.One)), ",")
    }
    jsonresult := fmt.Sprintf(`{"1":%s}`, one)
    return []byte(jsonresult), nil
}
func main() {
    results := []uint8{0, 0, 0, 0, 0, 0}
    j, _ := json.Marshal(&d{Data:o{One:results}})
    err := ioutil.WriteFile("output.json", []byte(j), 0777) // i can't test here I don't know if []byte(j) or []byte(string(j)) should be used
    if err != nil {
        log.Fatal(err)
    }
}

Upvotes: 1

Related Questions