SoFunction
Updated on 2025-03-05

The solution to the problem of converting the hollow slice into JSON and turning it into null

question

In Golang, it is often necessary to convert data from other types (such as slice, map, struct, etc.) into JSON format. Sometimes the conversion result is not expected, for example, when converting an empty slice into JSON, it becomes "null" instead of the expected "[]". The sample code is as follows:

package main
import (
	"encoding/json"
	"fmt"
)
func main() {
	var res []string
	b, err := (res)
	if err != nil {
		panic(err)
	}
	(string(b))
}

Run the example and see the results:

$ go run 
null

The output value of the result is "null", not the expected "[]".

reason

The res in the above example code is declared as a string type slice through the var keyword. Such a slice is called a zero-value slice, and its value is nil and does not point to any memory address. The slice in the actual title is a zero-value slice. Next, the zero-value slice and the empty slice are introduced.

In Golang, a slice is an array of variable lengths with three properties: pointer, length, and capacity. "Zero value slice" and "empty slice" are two special slices.

  1. Zero value slice: When a variable of slice type is declared but not explicitly initialized, its value is a zero value slice. The zero-value slice will not be allocated memory space, and the length and capacity are zero. The zero-value slice can also be said to be a nil slice. For example:
var s []string
(s == nil)  // Output "true"
  • Empty slice: The length and capacity of the empty slice are also zero, but point to a real, although empty, array that has already allocated memory. Empty slices can be created using make functions or literal syntax. For example:
s := make([]string, 0)
(s == nil)  // Output "false"
s := []string{}
(s == nil)  // Output "false"

In both examples, s is an empty slice, both length and capacity are zero, but the value is not nil.

Zero-value slices and empty slices are used interchangeably in most cases and can be used to represent an empty set. However, if you need to distinguish whether a slice has been explicitly initialized, you need to pay attention to their differences. The encoding/json library handles the two differently, encodes zero-value slices as "null" and empty slices as "[]". This can cause problems in some cases, such as when the interface expects "[]" rather than "null" for the processing of an array.

Having explained this, I believe everyone already knows the answer to the title of this article. If you convert an empty slice into JSON format and expect to get "[]", you need to use make function or literal syntax to create the slice when declaring the slice. See a simple example:

package main
import (
	"encoding/json"
	"fmt"
)
func main() {
	res := make([]string, 0)
	// Or res:= []string{}	// instead of var res []string	b, err := (res)
	if err != nil {
		panic(err)
	}
	(string(b))
}

Run the code to see the effect:

$ go run 
[]

It can be seen that the expected results have been achieved.

summary

This article explains the definition and difference between zero-value slices (nil slices) and empty slices. If you want to convert an empty slice to JSON format and get "[]" instead of "null", the best way is to use make functions or literal syntax to create slices.

The above is the detailed content of the solution to the problem of converting the hollow slice of Golang into JSON and turning into null. For more information about GolangJSON becoming null, please follow my other related articles!