Need help by parsing json config

Hey guys and girls, I’m new by using the go lang and I have a problem that I not understand.
i have a json config file named conf.json
and I have a main.go The main.go should import the config file and print out the struct.
But the Struct Config is empty.
Why? What is my failure?

Thanks in Advances

{
    "ca":{
        "enduser":["a_wi_basic_productsuite",
            "a_wi_basic_wb",
            "a_wi_end_ewd",
            "a_wi_end_fbf",
            "a_wi_end_wb",
            "a_wi_end_wt",
            "a_wi_basic_productsuite",
            "a_wi_basic_wb",
            "a_wi_end_ewd",
            "a_wi_end_fbf",
            "a_wi_end_star",
            "a_wi_end_wb",
            "a_wi_end_wt"],
        "keyuser":["rsi_wi_dev",
            "a_wi_key_wb",
            "a_wi_key_geof",
            "a_wi_key_geof",
            "a_wi_key_wb"]
    },
    "fv":{
        "enduser":["bFV_FLT_OI_END_ALL",
            "bFV_FLT_OI_END_ALL"],
        "keyuser":["bFV_FLT_OI_KEY_ALL",
            "FV_ALL_DEV_ALL",
            "a_FV_ALL_DEV_ALL"]
    },
    "ri":{
        "enduser":["a_ri_fv_end_fv",
            "a_ri_pz_light_advanced",
            "a_ri_pz_light_advanced",
            "a_ri_pz_light_basic",
            "a_ri_pz_light_basic",
            "a_ri_ro_end_bay",
            "a_ri_ro_end_bw",
            "a_ri_ro_end_mit",
            "a_ri_ro_end_no",
            "a_ri_ro_end_nord",
            "a_ri_ro_end_nrw",
            "a_ri_ro_end_sbb",
            "a_ri_ro_end_sbhh",
            "a_ri_ro_end_sbkhb",
            "a_ri_ro_end_sbm",
            "a_ri_ro_end_sbrhm",
            "a_ri_ro_end_sbs",
            "a_ri_ro_end_so",
            "a_ri_ro_end_systec",
            "a_ri_ro_end_wfb"],
        "keyuser":["a_ri_fbf_power",
            "a_ri_fv_key_fv",
            "a_ri_ro_key_bay",
            "a_ri_ro_key_bw",
            "a_ri_ro_key_mit",
            "a_ri_ro_key_no",
            "a_ri_ro_key_nord",
            "a_ri_ro_key_nrw",
            "a_ri_ro_key_sbb",
            "a_ri_ro_key_sbhh",
            "a_ri_ro_key_sbkhb",
            "a_ri_ro_key_sbm",
            "a_ri_ro_key_sbrhm",
            "a_ri_ro_key_sbs",
            "a_ri_ro_key_so",
            "a_ri_ro_key_systec",
            "a_ri_ro_key_wfb"]
    }
}
// ------- end conf.json ------


// ------ start main.go-------
package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type Config struct {
	Ca struct {
		Enduser string `json:"enduser"`
		Keyuser string `json:"keyuser"`
	} `json:"ca"`
	Fv struct {
		Enduser string `json:"enduser"`
		Keyuser string `json:"keyuser"`
	} `json:"fv"`
	Ri struct {
		Enduser string `json:"enduser"`
		Keyuser string `json:"keyuser"`
	} `json:"ri"`
}

func LoadConfiguration(file string) (Config, error) {
	var config Config
	configFile, err := os.Open(file)
	defer configFile.Close()
	if err != nil {
		fmt.Println(err.Error())
		panic("Config file not loadable")
	}
	jsonParser := json.NewDecoder(configFile)
	jsonParser.Decode(&config)
	return config, err
}

func main() {
	fmt.Println("Starting the Application....")
	config, _ := LoadConfiguration("./conf.json")
	fmt.Println(config)

}
//--------END main.go

//------ Screen Output ----
fl@Pearbook splunkuser % go run main.go            
Starting the Application....
{{ } { } { }}
fl@Pearbook splunkuser % ´´´

Check the returned error here.

It will tell you that it got an array where a string was expected, compare my playground conversion of your code:

3 Likes

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