Skip to content

Instantly share code, notes, and snippets.

@MarketingPip
Forked from tobiasrohloff/get_label.go
Created October 5, 2023 05:28
Show Gist options
  • Save MarketingPip/e748f657d6fdbe9731d278a1ca34f5ae to your computer and use it in GitHub Desktop.
Save MarketingPip/e748f657d6fdbe9731d278a1ca34f5ae to your computer and use it in GitHub Desktop.
Wikidata API: Get Label for Item ID
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
response, err := http.Get("https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q21&format=json&languages=en&props=labels")
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
value := make(map[string]interface{})
err = json.Unmarshal(body, &value)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s\n", (((value["entities"]).(map[string]interface{})["Q21"]).(map[string]interface{})["labels"]).(map[string]interface{})["en"].(map[string]interface{})["value"].(string))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment