Building a Client-Side JSON Formatter and Validator in Next.js
JSON tooling is one of those things every developer reaches for constantly — debugging API responses, reading config files, preparing request bodies. I built a JSON formatter and validator as part ...

Source: DEV Community
JSON tooling is one of those things every developer reaches for constantly — debugging API responses, reading config files, preparing request bodies. I built a JSON formatter and validator as part of Ultimate Tools and wanted to share how it works under the hood. Full feature set: real-time formatting, inline validation with error messages, 2-space / 4-space indent toggle, minify mode, one-click copy. All client-side. No API calls. No dependencies beyond React. The Core: JSON.parse + JSON.stringify The entire formatting logic is two native browser functions: const format = (input: string, indent: number): string => { const parsed = JSON.parse(input); return JSON.stringify(parsed, null, indent); }; JSON.parse validates and parses. JSON.stringify with the third argument (indent) produces formatted output. That's it. The indent argument accepts a number (spaces) or a string (e.g. '\t' for tabs). Real-Time Validation The formatter validates as the user types. We wrap JSON.parse in a try