A new Go API for JSON go.dev · 266 points · 120 comments · HN thread TL;DR The G...

@best30

A new Go API for JSON go.dev · 266 points · 120 comments · HN thread

TL;DR The Go team announces that encoding/json/v2 and encoding/json/jsontext are stable and generally available in Go 1.27, after being an experiment since Go 1.25. The post explains the long-standing flaws in encoding/json (invalid UTF-8 accepted, duplicate keys accepted, nil slices marshaled as null, case-insensitive field matching, costly MarshalJSON/UnmarshalJSON) and how v2 changes defaults and adds streaming, options, and v1 compatibility.

  • v2 rejects invalid UTF-8 and duplicate object member names by default, and marshals nil slices and maps as [] and {} instead of null.
  • Unmarshaling matches JSON member names to Go struct fields case-sensitively, and omitempty now omits null, "", [], and {}.
  • jsontext provides a syntax-only streaming Encoder/Decoder with tokens and values, without reflection; v2 adds MarshalJSONTo and UnmarshalJSONFrom so options propagate and parsing is not repeated.
  • Unmarshal is reported as up to 10x faster than v1, while Marshal is roughly at parity; v1 is reimplemented on top of v2 with compatibility options so the two interoperate.
  • The v2 effort took about six years, starting with a 2020 draft by Daniel Martí and a prototype with Joe Tsai, and was developed largely by people not employed by Google.

Discussion The thread drifts well beyond the JSON API into whether Go is still a good language to target with LLMs, with many commenters describing their own agentic coding habits and worries about skill atrophy. Within the JSON topic, the discussion is more practical: people welcome the cleaner API and the stricter defaults, debate whether nil slices becoming [] is correct, question the six-year timeline, and note that MarshalIndent's replacement is a bit awkward. The mood is mostly positive and appreciative, with some skepticism about Go's pace and type system.

  • Welcome the improved API and stricter defaults (many): Commenters are glad encoding/json/v2 fixes long-standing issues and that v1 remains supported. Several say the API improvements matter more than raw speed.
  • Nil slices as empty JSON arrays is the right default (some): One commenter argues that Go code usually wants nil and empty slices to behave the same, and that v1's null output caused frequent bugs. Another worries that this default will bite people later and need a v3 fix.
  • Six years is too long for a JSON API (some): Some question the Go team's velocity, citing generics and modules. Others reply that the JSON package was good enough for most users and that third-party libraries filled the gap.
  • Go is no longer the best choice for AI-assisted work (a few): Several comments discuss LLM-written Go, compile speed versus Rust, and whether Go is the right target language for models. This is a thread-level tangent rather than a view on the JSON change.
  • Keep hand-coding for skill and enjoyment (a few): A few commenters say they deliberately write code by hand to keep their skills sharp and to enjoy programming, arguing that atrophied skills make it harder to direct AI tools well.
  • Missing MarshalIndent is a migration pain (a few): One commenter wishes MarshalIndent had been kept for quick debug output. Replies point to jsontext.WithIndent and Value.Indent as the replacements.

Links from the comments

#go #json #encoding #stdlib #api #llm