Handling Null JSON Arrays in Golang (Go)

Handling Null JSON Arrays in Golang (Go)

One of the most frustrating things with Go is how it handles empty arrays when encoding JSON. Rather than returning what is traditionally expected, an empty array, it instead returns null.

One of the most frustrating things with Go is how it handles empty arrays when encoding JSON. Rather than returning what is traditionally expected, an empty array, it instead returns null. For example, the following:

package main

import (
    "encoding/json"
    "fmt"
)

// Bag holds items
type Bag struct {
    Items []string
}

// PrintJSON converts payload to JSON and prints it
func PrintJSON(payload interface{}) {
    response, _ := json.Marshal(payload)
    fmt.Printf("%s\n", response)
}


func main() {
    bag1 := Bag{}
    PrintJSON(bag1)
}

Outputs:

{"Items":null}

This occurs because of how the json package handles nil slices:

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**.

There are some proposals to amend the json package to handle nil slices:

But, as of this writing, these proposals have not been accepted. As such, in order to overcome the problem of null arrays, we have to set nil slices to empty slices.

tips-and-tricks go json golang

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to work with JSON data in Go?

This article covers the encoding/json package which handles conversion between JSON and Go types (as per RFC 7159).

Why Golang (Go) so Fast?

Go is praised by everyone for achieving extremely high concurrent performance, but Why? In this Golang (Go) tutorial, I’ll introduce how the Go program to achieve extremely high concurrent performance, and its internal scheduler implementation architecture (G-P-M model). I’ll also explain how Go can make full use of computing resources, and how the Go scheduler deals with threads step by step

Securing Your Golang (Go) REST APIs with JSON Web Tokens(JWTS)

In this tutorial, we’ll be looking at exactly what it takes to build a secure Go-based REST API that uses JSON Web Tokens to communicate!

Go Tutorial (Golang) - Structures in Go

Structs are the way to create user-defined types in Golang. One of the most useful data structures in computer science is the hash table. Many hash table implementations exist with varying properties, but in general they offer fast lookups, adds, and deletes. Go provides a built-in map type that implements a hash table.

Convert JSON To CSV In Golang with Example

The CSV package has a NewWriter() function that returns the Writer object which is used for writing the CSV data. The csv.Writer() writes csv records that are terminated by the newline and uses the comma as a field delimiter.