Skip to content

Instantly share code, notes, and snippets.

@spicydog
Last active August 9, 2022 02:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spicydog/54703add01e82e3c071482c2ce4e7c22 to your computer and use it in GitHub Desktop.
Save spicydog/54703add01e82e3c071482c2ce4e7c22 to your computer and use it in GitHub Desktop.
package main
import "encoding/json"
// Unescape UTF-8 string
// e.g. convert "\u0e27\u0e23\u0e0d\u0e32" to "วรญา"
func UnescapeString(str string) (unescapedString string) {
json.Unmarshal([]byte(`"`+str+`"`), &unescapedString)
return
}
@rubenhorn
Copy link

You need to escape any the " in the JSON:

package main

import (
	"encoding/json"
	"strings"
)

func UnescapeUTF8(inStr string) (outStr string, err error) {
	jsonStr := `"` + strings.ReplaceAll(inStr, `"`, `\"`) + `"`
	err = json.Unmarshal([]byte(jsonStr), &outStr)
	return
}

@jairforero
Copy link

rubenhorn excelent solutions very thanks! (y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment