Not reading json file using os.Open

Folks,
I was trying this code, basically reading a json file using os.Open and then use NewDecoder and Decode to parse the json and access the Go struct. Below is the code and json file

package main

import (
“encoding/json”
“fmt”
“os”
)
type Apprec struct {
Name string json:"name,omitempty"
Partition string json:"partition,omitempty"
TrafficGroup string json:"traffic_group,omitempty"
Tables struct {
ColumnNames string json:"columnNames"
Name string json:"name"
EncryptedColumns string json:"encryptedColumns"
Rows string json:"rows"
} json:"tables,omitempty"
}

func LoadConfiguration(filename string) (Apprec, error) {
var apprec Apprec
configFile, err := os.Open(filename)
fmt.Println(" I am in load funct and configFile is ", configFile)
defer configFile.Close()
if err != nil {
return apprec, err
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&apprec)
return apprec, err
}

func main() {
fmt.Println(" Starting iApps stuff …")
apprec, _ := LoadConfiguration(“http1.json”)
fmt.Println(apprec)
}

WhenI run the program I see a blank record

SJC-ML-SHITOLE:src shitole$ go run main.go
Starting iApps stuff …
I am in load funct and configFile is &{0xc4200820f0}
{ { }}

------- below is my json file http1.json -----

{

        "name": "top",
        "partition": "Common",
        "tables": [
            {
                "name": "basic__snatpool_members"
            },
            {
                "name": "net__snatpool_members"
            },
            {
                "name": "optimizations__hosts"
            },
            {
                "columnNames": [
                    "name"
                ],
                "name": "pool__hosts",
                "rows": [
                    {
                        "row": [
                            "ry.hj.com"
                        ]
                    }
                ]
            },
            {
                "columnNames": [
                    "addr",
                    "port",
                    "connection_limit"
                ],
                "name": "pool__members",
                "rows": [
                    {
                        "row": [
                            "",
                            "80",
                            "0"
                        ]
                    }
                ]
            },
            {
                "name": "server_pools__servers"
            }
        ],
         
        "traffic_group": "/Common/traffic-group-1",
         
         
      }

The error you did not check returned from LoadConfiguration reads:

invalid character '}' looking for beginning of object key string

The JSON you provided is not valid. I fixed it by removing the comma from the end of "traffic_group": "/Common/traffic-group-1"

After fixing that, a new error appears:

json: cannot unmarshal array into Go struct field Apprec.tables of type struct { ColumnNames string "json:\"columnNames\""; Name string "json:\"name\""; EncryptedColumns string "json:\"encryptedColumns\""; Rows string "json:\"rows\"" }

This is solved by defining Tables as a slice of struct:

Tables       []struct {

The next error is:

json: cannot unmarshal array into Go struct field .columnNames of type string

This is solved with:

ColumnNames      []string `json:"columnNames"`

The next error is:

json: cannot unmarshal array into Go struct field .rows of type string

This is solved by defining Rows as:

		Rows             []struct {
			Row []string `json:"row"`
		} `json:"rows"`

Which finally results in the output:

$ go run main.go
 Starting iApps stuff …
 I am in load funct and configFile is  &{0xc42006c190}
{top Common /Common/traffic-group-1 [{[] basic__snatpool_members  []} {[] net__snatpool_members  []} {[] optimizations__hosts  []} {[name] pool__hosts  [{[ry.hj.com]}]} {[addr port connection_limit] pool__members  [{[ 80 0]}]} {[] server_pools__servers  []}]}

Checking errors is extremely important. Most of these errors told me exactly what to work on to make the program do what was expected.

For future use, I compiled a list of tools that generate Go structs from a JSON example.

1 Like

@nathankerr
Great thank you very much your help, I spend lot of time to figure out what was going on.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.