-2

This is my first attempt, as a Go newbee, to access deeply nested values from a JSON file generated by the Wikipedia API using Go structs. Reading through all threads concerning Unmarshaling with Go didn't help much.

Json sample file (extracted from the Wikipedia API)

{
  "batchcomplete": "",
  "query": {
    "normalized": [
      {
        "from": "Go_(programming_language)",
        "to": "Go (programming language)"
      }
    ],
    "pages": {
      "25039021": {
        "pageid": 25039021,
        "ns": 0,
        "title": "Go (programming language)",
        "extract": "Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson."
      }
    }
  }
}

I want to access the values of title and extract. Its trivial using jq:

$ jq '.query.pages[] | .title, .extract' wikipedia-api-output
"Go (programming language)"
"Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson."

my latest failed attempt in go:

type Wiki struct {
    Query struct {
        Pages struct {
            Article struct {
                Title   string `json:"title`
                Extract string `json:"extract`
            } `json:"25039021"`
        } `json:"pages"`
    } `json:"query"`
}

func main() {

    jsonStr :=
        `{
            "batchcomplete": "",
            "query": {
              "normalized": [
                {
                  "from": "Go_(programming_language)",
                  "to": "Go (programming language)"
                }
              ],
              "pages": {
                "25039021": {
                  "pageid": 25039021,
                  "ns": 0,
                  "title": "Go (programming language)",
                  "extract": "Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson."
                }
              }
            }
        }`
    var wikiDescr Wiki
    err := json.Unmarshal([]byte(jsonStr), &wikiDescr)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(wikiDescr)

}

This returns the expected result, but the pageid (25039021) used to extract the required values is generated by Wikipedia and can not be guessed. Is there a way to wildcard that value or do I need to extract that pageid value first and then use it like in the code above?

ripat
  • 3,076
  • 6
  • 26
  • 38
  • 1
    If you don't like defining those custom structs you might want to try something this: https://github.com/valyala/fastjson – Coconut Sep 25 '20 at 16:19

1 Answers1

2

Use a map, specifically a map[string]Page where Page is your page data struct:

type Page struct {
    Title   string `json:"title"`
    Extract string `json:"extract"`
}

type Wiki struct {
    Query struct {
        Pages map[string]Page `json:"pages"`
    } `json:"query"`
}

Working example here (after fixing some typos in the code from the question): https://play.golang.org/p/z9Ngcae9O1F

Adrian
  • 42,911
  • 6
  • 107
  • 99