1

I hope someone could help me with this issue because I have been scratching my head for a while.

I have a project where I am trying to load json into a struct in go. I have followed exactly several tutorials online, but keep getting no data back and no error.

My json file is called page_data.json and looks like:

[
    {
        "page_title": "Page1",
        "page_description": "Introduction",
        "link": "example_link",
        "authors":
        [
            "Author1",
            "Author2",
            "Author3",
        ]
    },
    // second object, same as the first
]

But when I try the following in go:

package main

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

type PageData struct {
  Title string `json: "page_title"`
  Description string `json: "page_description"`
  Link string `json: "link"`
  Authors []string `json: "authors"`
}

func main() {
    var numPages int = LoadPageData("page_data.json")
    fmt.Printf("Num Pages: %d", numPages)
}

func LoadPageData(path string) int {
    jsonFile, err := os.Open(path)
    if err != nil {
        fmt.Println(err)
    }
    defer jsonFile.Close()
    byteValue, _ := ioutil.ReadAll(jsonFile)

    var pageList []PageData

    json.Unmarshal(byteValue, &pageList)
    return len(pageList)
}

the output I get is:

Num Pages: 0

4
  • 3
    In Go, always check for errors. For example, err = json.Unmarshal(byteValue, &pageList). Commented Oct 16, 2018 at 23:45
  • Thanks for this. It gave me exactly what I was looking for. The issue was the trailing comma on the last Author Commented Oct 17, 2018 at 0:00
  • Unfortunately now the array has the expected length, but the values of the PageData attributes are all empty strings for some reason Commented Oct 17, 2018 at 0:25
  • 2
    If you write a test or use go vet directly it will show you the next problem: bad syntax for struct tag value. Commented Oct 17, 2018 at 1:24

1 Answer 1

4

Fix the JSON commas and the Go struct field tags. For example,

package main

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

type PageData struct {
    Title       string   `json:"page_title"`
    Description string   `json:"page_description"`
    Link        string   `json:"link"`
    Authors     []string `json:"authors"`
}

func main() {
    var numPages int = LoadPageData("page_data.json")
    fmt.Printf("Num Pages: %d\n", numPages)
}

func LoadPageData(path string) int {
    jsonFile, err := os.Open(path)
    if err != nil {
        fmt.Println(err)
    }
    defer jsonFile.Close()
    byteValue, err := ioutil.ReadAll(jsonFile)
    if err != nil {
        fmt.Println(err)
    }
    var pageList []PageData

    err = json.Unmarshal(byteValue, &pageList)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(pageList)

    return len(pageList)
}

Output:

[{Page1 Introduction example_link [Author1 Author2 Author3]}]

page_data.json:

[
    {
        "page_title": "Page1",
        "page_description": "Introduction",
        "link": "example_link",
        "authors":
        [
            "Author1",
            "Author2",
            "Author3"
        ]
    }
]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.