skip to content

JSON Marshaling

encoding/json in both directions: tags that rename or drop fields, Decoder and Encoder streaming, custom MarshalJSON, RawMessage. It is the standard-library package Go interviews probe most.

part ofGo (Golang)overview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

In a json struct tag, what does the ,string option do to the field's encoded value?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

The ,string option makes encoding/json write the field's value inside a JSON string, so an int64 holding 63 encodes as "63" rather than 63, and requires a quoted value when decoding. It applies only to string, integer, floating-point and boolean fields.

open as a page

When may a JSON response field be removed if some clients can never be upgraded?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Only when evidence, not a code search, says nothing in the wild still reads it: traffic by client version measured against the release that stopped needing it. Until then keep emitting the key, and decide by what emitting it forces you to keep alive.

open as a page

When should a fleet move from encoding/json to encoding/json/v2, and who owns the traffic it starts rejecting?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Adopt it where strictness is the product -- inbound trust boundaries -- and leave interior and round-tripping code alone. It is an import change, so it stages per service. The team facing the sender owns the rejections, so they hold a veto and a documented per-call loosening option.

open as a page

showing 31–33 of 33