Skip to content

Unmarshaling failed when json string contains "\u0000", and it reports "invalid character '\x00' in string literal" #59140

@xiaobai-tx

Description

@xiaobai-tx

What version of Go are you using (go version)?

$ go version
go1.19.1

Does this issue reproduce with the latest release?

YES

What operating system and processor architecture are you using (go env)?

Linux

What did you do?

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
)

func UnescapeUnicode(uContent string) (string, error) {
	content := strings.Replace(strconv.Quote(uContent), `\\u`, `\u`, -1)
	text, err := strconv.Unquote(content)
	if err != nil {
		return "", err
	}
	return text, nil
}

func main() {
	v := "{\"aa\":\"some\\u0000\"}"
	v2, _ := UnescapeUnicode(v)
	var m = make(map[string]interface{})
	err := json.Unmarshal([]byte(v2), &m)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(m)
}

What did you expect to see?

map[aa:some]

What did you see instead?

invalid character '\x00' in string literal

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions