Skip to content

Go makes encoding/json/v2 stable, no build flag required

A rewritten JSON API ships as a normal standard-library package in Go 1.27, with stricter decoding and unmarshalling the Go team says is up to 10x faster.

Source: The Go Blog

The Go team announced on 4 November that encoding/json/v2 and its companion package encoding/json/jsontext are stable and generally available in Go 1.27. They were introduced as an experiment in Go 1.25; now they’re a normal import, no GOEXPERIMENT flag needed.

The new package is opt-in: encoding/json keeps working exactly as before, and the team says v1 is now implemented in terms of v2, so both can be imported side by side. What changes if you switch is behaviour as well as speed. v2 rejects invalid UTF-8 and duplicate object member names, where v1 silently accepted both. It unmarshals into structs with case-sensitive field matching instead of v1’s case-insensitive lookup, marshals nil slices and maps as [] and {} instead of null, and redefines omitempty to drop any field that encodes as an empty JSON value. Marshalling is roughly at parity with v1; unmarshalling is, in the team’s own benchmarks, up to 10x faster.

import "encoding/json/v2"

Why it matters: the speed gain alone is worth checking on unmarshal-heavy code, but the behaviour changes are the part to read closely before migrating. Case-sensitive struct matching and strict UTF-8 validation can turn previously-tolerated malformed input into runtime errors, which is good for correctness but will need testing against real payloads first.

Share

Related reading

More from The Wire

All briefs