Picture a short program that should make you pause. You marshal a slice of structs, one of which has a nil slice field. On v1 the output contains "Nicknames":null. Switch the import to encoding/json/v2 and it prints "Nicknames":[]. The code compiles either way, the tests you wrote for the happy path still pass, and a downstream service that treats null and [] differently quietly starts misbehaving.

That example comes straight from Go's own migration guide, and it is the right mental model for the whole upgrade. The Go team published its long write-up of the new JSON API on November 4, announcing that encoding/json/v2 and encoding/json/jsontext are stable and generally available in Go 1.27. The API is the easy part. The behaviour is where you can get hurt. I haven't migrated a codebase myself, so what follows is my reading of the Go blog post and the migration guide, plus a plan for doing it without drama.

First, you don't have to move

The guide opens with the line I wish more migration docs started with: you don't have to. encoding/json will never go away, it is covered by the Go 1 compatibility promise, and applications using it will keep working indefinitely. The two packages are also compatible with each other. If a type implements the new MarshalJSONTo, callers going through the old json.Marshal still reach the custom marshaler, and the new struct tags work in v1 too.

There is a quieter detail underneath. In Go 1.27 the old package is implemented in terms of the new one. v1 is just v2 called with the full set of v1 compatibility options. That means you already run v2 code today, and it is the reason the migration can be gradual.

What actually changes by default

The blog post and the guide agree on the list. I have put the v1 and v2 behaviours side by side, along with the option that flips each one back, so you can grep your code for the places each could bite.

Situation encoding/json (v1) encoding/json/v2
Invalid UTF-8 inside a string Replaced silently with the Unicode replacement character Returns an error
Object with duplicate member names Accepted Returns an error
Nil slice or nil map on marshal JSON null Empty array or empty object
Matching JSON keys to struct fields Case-insensitive Case-sensitive
The omitempty tag option Original v1 rules Omits any field that would encode as null, "", [] or {}
Marshaling a time.Duration Encodes without an error Returns an error unless you choose an option
Malformed struct tags or invalid types No runtime error Runtime error

One thing the table makes obvious is that every row is a stricter or more explicit rule. None of them are bugs in v1 that Go quietly fixed. Each is a place where v1 guessed, and v2 makes you decide. Audit your code for the three rows most likely to touch real traffic: nil slices, case-sensitive keys and omitempty.

Why the strictness matters outside Go

The Go team's own argument for the duplicate-name rule is a security one. JSON with duplicate names has no universally agreed meaning, an implementation may take the last value, merge or error, and the post notes this has been exploited before, citing CVE-2017-12635. If your Go service and a gateway in front of it disagree about which "role" wins, an attacker gets to choose the one that suits them. Rejecting duplicates by default is the safe side of that argument.

The case-insensitive matching has the same flavour. The post calls it a surprising default, a potential security vulnerability and a performance limitation. If a payload with "Admin": true and a struct field called admin used to line up, that behaviour is now opt-in through MatchCaseInsensitiveNames.

Rows of servers in a Wikimedia Foundation data center

Expand

Rows of servers in a Wikimedia Foundation data center

Image: Victorgrigas via Wikimedia Commons, CC BY-SA 3.0

Go programming language blue logo

Expand

Go programming language blue logo

Image: The Go Authors via Wikimedia Commons, Public domain

The three migration routes

The guide lays out three approaches and tells you to match them to your risk tolerance.

All at once, if the consequences of a mistake are small. Change the import, run the tests, merge. If something differs, the error or the changed output usually points at the cause, and you set the matching compatibility option.

Option by option, if you're cautious. Start by moving every call to v2 with DefaultOptionsV1. The guide calls this trivial and safe, and points out that it is exactly how encoding/json implements Marshal and Unmarshal. Later options override earlier ones, so you then switch off v1 behaviours one at a time:

GoCopy 

b, err := json.Marshal(v, jsonv1.DefaultOptionsV1(), json.FormatNilSliceAsNull(false))

That call behaves like v1 except that nil slices become empty arrays. You can enable one change per commit and know precisely what each did. It is also a decent way to bisect when something breaks in staging.

Production servers: use jsonsplit. This is a wrapper package, github.com/go-json-experiment/jsonsplit, that behaves like encoding/json by default but can be told to run v1, v2 or both. In CallBothButReturnV1 mode it marshals every value twice, reports any difference, and still hands v1's output to your caller, so production behaviour doesn't change. With AutoDetectOptions it even names the option responsible. In the guide's example the report says the difference came from jsonv2.FormatNilSliceAsNull.

A rollout plan I would actually use

The order the guide recommends for the jsonsplit route, trimmed to the steps that matter:

  1. Swap call sites to jsonsplit, set CallBothButReturnV1, and wire ReportDifference into your logs or metrics.
  2. Watch production for reported differences.
  3. Resolve each one by passing the matching option, or by fixing a type. For example, the string struct tag on an invalid type is ignored in v1 and an error in v2, and the guide says to drop the tag rather than suppress the error, since it wasn't doing anything.
  4. When the reports stop, switch to OnlyCallV2, or CallBothButReturnV2 if you want to keep checking.
  5. Clean up: replace jsonsplit with encoding/json/v2 itself, then revisit the places where you kept v1 behaviour.

There is a cost. Marshaling both ways roughly doubles marshaling work, and auto-detection adds more marshals. The guide's mitigation is SetMarshalCallRatio, which checks only a random subset of calls. For a high-traffic API I'd start at a low ratio and raise it.

One more point from the guide that is easy to miss: a difference in output doesn't mean downstream behaviour is broken, only that there is a chance of breakage. Don't spend a week arguing about each null versus []. Preserve the v1 output first, finish the migration, then come back and decide which places can take the new behaviour.

Where v2 pays you back

The reasons the Go team gives for migrating are mostly about ergonomics and speed:

  • MarshalWrite and UnmarshalRead work directly on an io.Writer or io.Reader, without constructing an Encoder or Decoder. The post also notes that json.NewDecoder(r).Decode(v) fails to reject trailing junk, which the new functions avoid.
  • Options are a first-class argument to each call, rather than something you can only set on an Encoder.
  • Marshalers lets you override how particular types are marshaled even when you don't control those types. The blog's example is routing every proto.Message through protojson.
  • Unmarshal is the big performance story: the post reports speedups of up to 10x, while saying Marshal is roughly at parity, sometimes a little faster and sometimes a little slower.

The speedups for custom types need work on your side. Types that implement MarshalJSON and UnmarshalJSON only get the streaming benefits if they also implement MarshalJSONTo and UnmarshalJSONFrom. The post gives a Kubernetes example where recursive parsing of OpenAPI specs inside an UnmarshalJSON method hurt performance, and switching to UnmarshalJSONFrom improved it by orders of magnitude. If you have a hot custom type, that is where to look first. The Go team also says it expects further optimizations in Go 1.28 for cases where v2 is currently slower.

What I'd do this week

If you maintain a library, don't force v2 on your users. Since v1 and v2 interoperate, you can adopt the new interfaces at your own pace. If you run a service, make the DefaultOptionsV1 change first, because it is a no-op in behaviour and leaves you with call sites you can flip one option at a time.

And before anything else, search your repository for omitempty, for tests that compare raw JSON strings, and for places that unmarshal data from partners with inconsistent key casing. Those three searches will find most of what v2 is going to disagree with you about.

Frequently Asked Questions

Do I need to change my imports to get any of this?

No. Go 1.27 already runs the old encoding/json on the v2 implementation with v1 options. You only import encoding/json/v2 when you want the new defaults or the new API.

Can my package use v2 while a dependency still uses v1?

Yes. Types with v1 MarshalJSON methods work under v2, and types with v2 MarshalJSONTo methods work under v1, so mixed codebases are supported by design.

Is the speedup guaranteed for my service?

No. The post says Unmarshal benchmarks show gains of up to 10x and Marshal is roughly at parity. Your result depends on your types and payloads, so measure with your own data.

Originally published on Mango Developer.