Go's JSON v2 changes how empty PATCH payloads serialize, with omitempty behavior shifting

A developer found that the same Go struct with omitempty tags produced an empty object under encoding/json but {"count":0,"enabled":false} under encoding/json/v2. The difference matters for PATCH-style endpoints, where an absent field means leave the value unchanged and a present zero value should replace it. In v2, omitempty is based on the encoded JSON value, so zero and false are retained, while empty arrays can be dropped, which can hide an intended list clear. The author recommends pointer fields and omitzero tags to distinguish absent values from explicit zeros or empty lists. The approach was verified with a small serialization test on Go 1.27.3, which is not a running HTTP service. Go announced encoding/json/v2 will be generally available in Go 1.27.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in