Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json unmarshal "null" to empty slice variable(etc: []string{} ) doesn't have err. But when marshal the variable, the value will be "null" instead of "[]" #26866

Closed
Tron2016 opened this issue Aug 8, 2018 · 1 comment

Comments

@Tron2016
Copy link

Tron2016 commented Aug 8, 2018

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

go version go1.10.2

Does this issue reproduce with the latest release?

yes

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

linux/amd64

What did you do?

ls := []string{}
if err := json.Unmarshal([]byte("null"), &ls); err != nil {
	fmt.Println(err)
}
fmt.Println(ls)
if result, err := json.Marshal(ls); err != nil {
	fmt.Println(err)
} else {
	fmt.Println(string(result))
}

What did you expect to see?

output:
[]
[]

What did you see instead?

output
[]
null

@Tron2016 Tron2016 changed the title json unmarshal "null" to slice verable(etx ) don't have err. But when mash the v json unmarshal "null" to empty slice verable(etc: []string{} ) don't have err. But when mash the verable, the value will be "null" instead of "[]" Aug 8, 2018
@Tron2016 Tron2016 changed the title json unmarshal "null" to empty slice verable(etc: []string{} ) don't have err. But when mash the verable, the value will be "null" instead of "[]" json unmarshal "null" to empty slice variable(etc: []string{} ) doesn't have err. But when mash the variable, the value will be "null" instead of "[]" Aug 8, 2018
@Tron2016 Tron2016 changed the title json unmarshal "null" to empty slice variable(etc: []string{} ) doesn't have err. But when mash the variable, the value will be "null" instead of "[]" json unmarshal "null" to empty slice variable(etc: []string{} ) doesn't have err. But when mashal the variable, the value will be "null" instead of "[]" Aug 8, 2018
@Tron2016 Tron2016 changed the title json unmarshal "null" to empty slice variable(etc: []string{} ) doesn't have err. But when mashal the variable, the value will be "null" instead of "[]" json unmarshal "null" to empty slice variable(etc: []string{} ) doesn't have err. But when marshal the variable, the value will be "null" instead of "[]" Aug 8, 2018
@mvdan
Copy link
Member

mvdan commented Aug 8, 2018

This is a common misconception in the encoding/json package, but it is very clearly documented:

Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.

If you want to marshal a slice as an empty JSON array, you need to make the slice be non-nil. This has been covered at length before: https://stackoverflow.com/questions/33183071/golang-serialize-deserialize-an-empty-array-not-as-null

@mvdan mvdan closed this as completed Aug 8, 2018
YutoMizutani added a commit to YutoMizutani/SwiftyHome that referenced this issue Feb 25, 2019
Golangってempty slice がnull JSONになるみたい。golang/go#26866
@golang golang locked and limited conversation to collaborators Aug 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants