#Go JSON nested

11 messages · Page 1 of 1 (latest)

burnt dagger
#

You can actually Unmarshal the JSON, and after use json.MarshalIndent() function

#

Like this:

var data StructJson

err := json.Unmarshal([]byte(jsonData), &data)
if err != nil {
  fmt.Println("Error unmarshalling JSON:", err)
  return
}

prettyJSON, err := json.MarshalIndent(data, "", "  ")
if err != nil {
  fmt.Println("Error marshalling JSON:", err)
  return
}

fmt.Printf("%s\n", prettyJSON)
#

I've made one version to debug and test this

#

And thats my output:

burnt dagger
#

You are trying to Unmarshal the URL instead of the response body in this line: err := json.Unmarshal([]byte(url), &data)

burnt dagger
#

Instead of the ioutil you can continue using the NewDecoder, like this:

func main() {
    url := "https://gameinfo.albiononline.com/api/gameinfo/events?limit=51&offset=0&guildId=BXD9YKeuQbmE3qVfxhqNRg"
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println("Error making HTTP request:", err)
        return
    }
    defer resp.Body.Close()

    var data AutoGenerated
    if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
        fmt.Println("Error decoding JSON:", err)
        return
    }

    prettyJSON, err := json.MarshalIndent(data, "", "  ")
    if err != nil {
        fmt.Println("Error marshalling JSON:", err)
        return
    }

    fmt.Printf("%s\n", prettyJSON)

}
#

I dont understand your second problem, srry

burnt dagger
#

Hmm

#

You can actually use a for range loop

#

Like this