Search code examples
gomediawikimediawiki-api

Unmarshaling values of JSON objects


If given the string, from a MediaWiki API request:

str = ` {
    "query": {
        "pages": {
            "66984": {
                "pageid": 66984,
                "ns": 0,
                "title": "Main Page",
                "touched": "2012-11-23T06:44:22Z",
                "lastrevid": 1347044,
                "counter": "",
                "length": 28,
                "redirect": "",
                "starttimestamp": "2012-12-15T05:21:21Z",
                "edittoken": "bd7d4a61cc4ce6489e68c21259e6e416+\\"
            }
        }
    }
}`

What can be done to get the edittoken, using Go's json package (keep in mind the 66984 number will continually change)?


Solution

  • Note that if you use the &indexpageids=true parameter in the API request URL, the result will contain a "pageids" array, like so:

    str = ` {
        "query": {
            "pageids": [
                "66984"
            ],
            "pages": {
                "66984": {
                    "pageid": 66984,
                    "ns": 0,
                    "title": "Main Page",
                    "touched": "2012-11-23T06:44:22Z",
                    "lastrevid": 1347044,
                    "counter": "",
                    "length": 28,
                    "redirect": "",
                    "starttimestamp": "2012-12-15T05:21:21Z",
                    "edittoken": "bd7d4a61cc4ce6489e68c21259e6e416+\\"
                }
            }
        }
    }`
    

    so you can use pageids[0] to access the continually changing number, which will likely make things easier.