
Go 1.27 adds two packages to the standard library: encoding/json/v2 and encoding/json/jsontext. Together they are the first major rework of JSON handling in Go, and they took about six years to go from the first prototype to a stable release. If you write REST APIs, CLI tools, webhooks or Kubernetes controllers in Go, JSON is probably on your hot path. That makes the headline result worth a look: decoding is much faster, with the Go team reporting benchmark gains of up to 10x, and the new defaults remove several long-standing surprises. Your existing code keeps working, because the original encoding/json package stays.
What the two new packages do
The work is split into two layers.
- encoding/json/jsontext handles JSON syntax only. It does not use reflection. Its streaming
EncoderandDecoderread and write tokens and values throughReadToken,WriteToken,ReadValueandWriteValue. A raw JSON value is thejsontext.Valuetype, and the oldjson.RawMessageis now an alias for it. - encoding/json/v2 handles semantics: how Go structs, maps and slices map to JSON and back. Its API deliberately looks like v1, so most code moves across with few edits.
The main functions in v2 are:
func Marshal(in any, opts ...Options) (out []byte, err error)
func MarshalWrite(out io.Writer, in any, opts ...Options) error
func MarshalEncode(out *jsontext.Encoder, in any, opts ...Options) error
func Unmarshal(in []byte, out any, opts ...Options) error
func UnmarshalRead(in io.Reader, out any, opts ...Options) error
func UnmarshalDecode(in *jsontext.Decoder, out any, opts ...Options) error
Every function now takes options as a variadic argument, so you no longer need to build an Encoder just to change one setting. MarshalWrite and UnmarshalRead work directly with writers and readers such as HTTP bodies and files.
Behaviour differences you need to know
The v2 defaults are stricter and more predictable. Some of the changes are visible to anyone who consumes your API:
| Situation | v1 | v2 |
|---|---|---|
| Invalid UTF-8 | Accepted | Error |
| Duplicate object names | Accepted | Error |
| Nil slice or map | null |
[] or {} |
| Field name matching | Case-insensitive | Case-sensitive |
omitempty |
Based on Go zero values | Omits fields that encode as null, "", [] or {} |
time.Duration |
Encoded as a number | Error unless you choose a format with an option |
Nil slices and case sensitivity are the two most likely to catch you out. A client that checks for null will now get an empty array. A request body that sends userName will no longer fill a field that expects username.
Where the speed comes from
The Go team says marshalling performs about the same as v1, sometimes a little faster and sometimes a little slower. Unmarshalling is where v2 is much faster, by up to 10x in their benchmarks. You can gain more by moving custom types from the old buffer-based MarshalJSON and UnmarshalJSON methods to the new streaming interfaces, MarshalJSONTo and UnmarshalJSONFrom. In one Kubernetes case cited by the Go team, this switch made decoding faster by orders of magnitude. Work on the slower cases continues, and more speed-ups are planned for Go 1.28.
v1 now runs on top of v2
The original encoding/json has been rebuilt as v2 with a full set of v1-compatibility options turned on. This has some useful effects:
- v1 is still covered by the Go 1 compatibility promise, so you never have to migrate.
- Some new features, such as the
MarshalJSONTointerface, also work for code that still imports v1. - The two versions work together. A v2 call will use a type’s v1
MarshalJSONmethod, and the reverse also works, so you can migrate one package at a time.
How to try it in your project
- Check that your toolchain is Go 1.27 or newer with
go version. - Raise the language version in your module:
go mod edit -go=1.27. - Import the new package next to the old one while you migrate:
import (
jsonv1 "encoding/json"
"encoding/json/v2"
"encoding/json/jsontext"
)
var cfg Config
if err := json.UnmarshalRead(r.Body, &cfg); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
If your code relies on one specific v1 behaviour, keep only that behaviour and use v2 for everything else:
// v2 defaults, but still allow duplicate names
json.Marshal(v, json.DefaultOptionsV2(), jsontext.AllowDuplicateName(true))
// v1 behaviour, but reject duplicate names
json.Marshal(v, jsonv1.DefaultOptionsV1(), jsontext.AllowDuplicateName(false))
Before you ship, work through this checklist:
- Run your API contract tests and look for
nullvalues that have become[]or{}. - Search for struct fields whose JSON names differ only by case from what clients send.
- Find every
time.Durationfield and choose an explicit format for it. - Log decode errors for a while after release, because rejected duplicate keys and invalid UTF-8 can expose clients that have been sending bad data.
- Read the official migration guide at
go.dev/doc/jsonv2-migrationfor less common cases.
How TechProvidence can help
Moving a production service to a new Go release involves CI pipelines, container images, staged rollouts and monitoring, as well as the code change itself. TechProvidence builds and runs DevOps pipelines and manages Linux and AWS infrastructure for Go applications, from first build to zero-downtime deploys. If you want help planning a Go 1.27 upgrade or tuning an API that spends too long decoding JSON, contact us.
Source: The Go Blog


