Skip to content

JSON Minify — Go source

Remove all whitespace from JSON for the most compact serialization. Validates as it minifies, runs entirely in your browser, with a shareable link to your exact input.

This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.

// Package jsonminify is the Go twin of CosmoDev's src/lib/json-minify.ts (dual
// source: web lib is TypeScript, CLI lib is Go — kept in lock-step). Pure +
// deterministic, never panics. Table-driven tests share vectors with the TS suite.
package jsonminify

import (
	"bytes"
	"encoding/json"
)

// Minify compacts JSON to its smallest valid form, returning ok=false on
// invalid JSON. Mirrors minifyJson() in json-minify.ts.
func Minify(s string) (string, bool) {
	var buf bytes.Buffer
	if err := json.Compact(&buf, []byte(s)); err != nil {
		return "", false
	}
	return buf.String(), true
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →