Parse key containing unicode characters in JSON with Go

Viewed 90

I have an API response like this:

{ 
  "Pass ✔": true
}

In Go I use this code:

type Status struct {
  Pass bool `json:"Pass ✔"`
}

// ...

var s Status

json.Unmarshal(body, &s)

fmt.Println(s.Pass) // false, where it should be true

How can I correctly unmarshal this JSON document?

1 Answers

As others mentioned, it's not currently possible to do that. As a workaround, you could do something like this:

package main

import (
   "encoding/json"
   "fmt"
)

type status map[string]bool

func (s status) pass() bool {
   return s["Pass ✔"]
}

func main() {
   data := []byte(`{"Pass ✔": true}`)
   var stat status
   json.Unmarshal(data, &stat)
   pass := stat.pass()
   fmt.Println(pass) // true
}
Related