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",
}