Home > Backend Development > Golang > How to Effectively Remove Invalid UTF-8 Characters from JSON Strings in Go?

How to Effectively Remove Invalid UTF-8 Characters from JSON Strings in Go?

DDD
Release: 2024-12-07 19:40:17
Original
522 people have browsed it

How to Effectively Remove Invalid UTF-8 Characters from JSON Strings in Go?

Stripping Invalid UTF-8 Characters from JSON Strings in Go

When encountering invalid UTF-8 characters in strings during JSON marshaling, a common issue in Go, it's crucial to find an effective way to remove or handle them.

In Go, various packages and techniques can be employed to address this issue. One straightforward option introduced in Go 1.13 is:

strings.ToValidUTF8("a\xc5z", "")
Copy after login

This function replaces invalid UTF-8 sequences with the replacement string specified as the second parameter.

Alternatively, Go 1.11 and later provide a versatile approach using the Map function and the utf8.RuneError constant:

fixUtf := func(r rune) rune {
    if r == utf8.RuneError {
        return -1
    }
    return r
}

fmt.Println(strings.Map(fixUtf, "a\xc5z"))
Copy after login

The strings.Map function applies the specified function to each rune in the string, returning a new string. The fixUtf function checks for invalid characters and replaces them with -1, effectively removing them from the output.

Using these methods, developers can quickly and reliably handle invalid UTF-8 characters in JSON strings, ensuring valid UTF-8 data during marshaling.

The above is the detailed content of How to Effectively Remove Invalid UTF-8 Characters from JSON Strings in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template