json-colorizer
A tiny TypeScript library that syntax-highlights and pretty-prints JSON for readable console output.
Repository Health
Technical Analysis
json-colorizer is a small TypeScript library that takes a JSON string or JavaScript object and returns a colorized, pretty-printed string ready to print to the console. It tokenizes the JSON input with a hand-written lexer, then maps each token type — braces, brackets, keys, strings, numbers, booleans, and null — to a customizable color function powered by colorette.
The library exposes a single colorize() function with sensible defaults (2-space indentation, a built-in color theme) while allowing full control over indentation and per-token-type color overrides via the exported color object. It has no runtime dependencies beyond colorette, ships full TypeScript types, and is commonly used in CLI tools and debug logging to make JSON output easier to scan.
What You Get
- A single colorize(json, options) function that accepts a JSON string or object and returns a color-coded, pretty-printed string
- A custom JSON lexer (tokenize) that classifies every character span into braces, brackets, colons, commas, keys, strings, numbers, booleans, and null tokens
- A default color theme mapping each token type to a colorette color function, fully overridable per call via the colors option
- Full TypeScript type definitions, including the exported ColorTheme and ColorizeOptions types
Common Use Cases
- Pretty-printing JSON payloads in CLI tool output so keys, strings, and values are visually distinct
- Highlighting JSON in debug and log statements during Node.js development
- Building custom REPLs or dev servers that need readable JSON console output
- Theming JSON output to match a tool’s existing terminal color scheme
Under The Hood
Architecture json-colorizer’s architecture is a minimal two-stage pipeline: src/lexer.ts exports a single tokenize() function that walks the raw JSON string character-by-character, matching an ordered list of regexes (whitespace, braces, brackets, colon, comma, number, string-key look-ahead, string, boolean, null) to produce a flat array of typed Token objects; src/index.ts then consumes that token stream in colorize(), merging a default color theme with any caller-supplied colors override and reducing the tokens into a single ANSI-colored string via Array.reduce. There is no class hierarchy, dependency injection, or internal state beyond the two pure functions — the only meaningful abstraction boundary is lexer vs. renderer, and the only external dependency crossing that boundary is colorette’s color functions, looked up by token type in a plain object map. The codebase is under 150 lines total, so the blast radius of any change to the core TokenType union stays small and localized to the two files.
Tech Stack The library is written in TypeScript (target ES2020, strict mode enabled) and compiled with the stock tsc compiler to CommonJS output in dist/, with a prepublishOnly script running the build before every npm publish. Its only runtime dependency is colorette (^2.0.20) for ANSI terminal coloring — there is no bundler, no test runner, and no linter or formatter configuration present in the repository. Distribution is a plain npm package (package.json main/types pointing at dist/index.js and dist/index.d.ts), consumed via require(‘json-colorizer’) or an ES import, with no build-time code generation or external service integrations.
Code Quality No test files, test framework, or CI workflow configuration exist anywhere in the repository — correctness relies entirely on manual verification and the TypeScript compiler, and this should be stated explicitly rather than assumed. That said, tsconfig.json enables strict mode, and both source files use precise discriminated-union types (TokenType, Token, ColorTheme) that turn an unmapped token type into a compile-time error rather than a runtime one. Naming is consistent and descriptive (tokenize, colorize, getJsonString), error handling is limited to a single explicit throw for an unmatched character in the lexer loop, and there is no linter or formatter configuration checked into the repo.
API Design The public API surface is intentionally tiny: a single colorize(json, options) function plus an exported color object re-exporting colorette’s color functions, so first use requires no setup beyond importing the package. Options are shallow-merged with sensible defaults (2-space indent, a built-in theme), and per-token-type color overrides reuse the same TokenType keys as the internal lexer, giving consumers compiler-checked autocomplete for valid override keys. This ergonomic single-function design is standard for small terminal-formatting utilities and comparable to similar JSON/console-highlighting packages — solid and low-friction, but not a novel approach.
Used by 2 apps in this directory
Dub
Marketing · Analytics
The open-source link attribution platform for short links, conversion tracking, and affiliate programs — powering 100M+ clicks monthly.
Puter
File Storage · Developer Tools
A self-hostable, web-based internet OS with desktop GUI, cloud storage, AI drivers, and a developer SDK — all running in your browser.