Getting JSON information into a multidimensional string var

I’m getting this info from a JSON file:

{
    "info": [
      {
        "type": "general",
        "news": [
          { "name": "abc",  "read": true },
          { "name": "def",  "read": true }
        ]
      },
      {
        "type": "confidential",
        "news": [
          { "name": "xxx",  "read": false },
          { "name": "yyy",  "read": false }
        ]
      }
    ]
}

And this is the function used to extract the JSON information

func ReadFile(file_name string) [ ][ ]string {

	var getInfo [ ][ ]string

	type News struct {
		Name    string  `json:"name"`
		Read     bool    `json:"read"`
	}

	type Info struct {
		Type  string  `json:"type"`
		News News `json:"news"`
	}

	type Information struct {
		Info  [ ]Info `json:"info"`
	}

	// Open file
	jsonFile, err := os.Open(file_name)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Successfully Opened file_name.json")
	// defer the closing of our jsonFile so that we can parse it later on
	defer jsonFile.Close()

	// read our opened file_name as a byte array.
	byteValue, _ := ioutil.ReadAll(jsonFile)

	var infor Information

	// we unmarshal our byteArray which contains our
	// jsonFile's content into 'Infor' which we defined above
	json.Unmarshal(byteValue, &infor)

	for i := 0; i < len(infor.Information); i++ {
		getInfo[i] = append(getInfo[i], infor.Information[i].Type)
		for j := 0; j < len(infor.Information.News); j++ {
			getInfo[i][j] = append(getInfo, infor.Information[i].News[j].Name)
			getInfo[i][j+1] = append(getInfo, infor.Information[i].News[j].Read)
		}
	}
	return getInfo
}

I am not sure if this is the best way to extract the JSON information as I have different “types” and looks like I cannot append like that as I’m getting an error regarding the type of variable i’m trying to append.

Basically I’m trying to get this info into the getInfo var:

getInfo = [general][abc, true, def, true]
          [confidential][xxx, false, yyy, false]
  1. You can easily unmarshal your JSON into a []Info.

is not what a [][]string would look like. If you actually do want a [][]string, maybe you mean for it to look something like:
`[[“general”,“abc, true, def, true”], [“confidential”,“xxx, false, yyy, false”]

  1. I recommend you unmarshal as in 1) and transform the []Info to your desired type(s).

Thanks for your answer.

Finally I’m doing something like this but it’s complaining that “Environments” is undeclared in the ReadFile function.

I declared the Environments struct in the main function and just passing the filename as an argument when calling the ReadFile function:

func main () {

type Information struct {
    Info []Info `json:"info"`
}

type Info struct {
    Type string `json:"type"`
    News []New  `json:"news"` 
}

type New struct {
    Name string `json:"name"`
    Read bool   `json:"read"`
}

file_name := "file.json"
reading := ReadFile(file_name)

This is the ReadFile function:

func ReadFile(file_name string) *Information {
    jsonFile, err := os.Open(file_name)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Successfully Opened file.json")

    defer jsonFile.Close()

    byteValue, _ := ioutil.ReadAll(jsonFile)

    var infor Information

    json.Unmarshal(byteValue, &infor)

    return &infor
}

I’m not sure if this is a runtime error.

You don’t mention the problem. Your code compiles for me, it must be a runtime error. I expect it is due to your ignoring errors.

Please, check it out again. I edited the message.

It is already resolved. I had to declare the struct outside the main function.

1 Like

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