DEV Community

Aquarius
Aquarius

Posted on

πŸ” Behind the Scenes: How I Built Client-Side JSON Tools (Diff, Patch, Format...)

Modern devs deal with JSON every single day β€” API responses, config files, database dumps, you name it.

But:

  • Comparing JSON? Not fun.
  • Patching JSON? Even worse.
  • Formatting or validating? Sometimes even that feels clunky.

So I built a single-page dev toolbox at πŸ‘‰ ulidtool.net/#json-tools

Here’s how it works under the hood β€” and the dev challenges behind each tool.


🧠 1. JSON Patch Generator (RFC 6902)

πŸ“Œ The Goal:

Let users compare two JSON documents and generate a list of patch operations (add, remove, replace) that convert A β†’ B.

βš™οΈ How it's built:

I use jsondiffpatch under the hood to deeply compare two objects.

import { compare } from 'jsondiffpatch';

const delta = compare(oldJson, newJson);
Enter fullscreen mode Exit fullscreen mode

This returns a structured diff. But RFC 6902 expects flat paths and strict op formats like:

{ "op": "replace", "path": "/user/0/name", "value": "Alice" }
Enter fullscreen mode Exit fullscreen mode

πŸ”„ The Challenge:

  • Traverse jsondiffpatch output
  • Detect type of change (add, remove, replace)
  • Convert nested paths into string paths
  • Escape / as ~1 (RFC spec compliance)

βœ”οΈ Solved using a recursive walker that tracks full key paths and outputs spec-compliant JSON Patch array.


πŸ”Ž 2. JSON Diff Viewer

🎯 Goal:

Let devs visually spot changes in JSON.

πŸ’‘ How:

  • I use CodeMirror to render syntax-highlighted, line-numbered JSON
  • Then compare the line-by-line diff using a smart algorithm
  • Mark inserted / removed / changed lines using diff-match-patch-like logic

Two modes:

  • Split view (left/right side-by-side)
  • Unified view (like GitHub)

This is one of the most used features in my toolset.


✨ 3. JSON Formatter & Minifier

🧩 Features:

  • Pretty-print JSON with spacing
  • Minify JSON (remove whitespace)
  • Beautify even ugly single-line blobs

βš™οΈ How it's built:

const formatted = JSON.stringify(JSON.parse(input), null, 2); // for beautify
const minified = JSON.stringify(JSON.parse(input)); // for minify
Enter fullscreen mode Exit fullscreen mode

Yes, it’s that simple β€” but the magic is in:

  • Clean error messages (invalid JSON)
  • Auto-fix common issues (’ instead of " etc.)
  • Preview mode

βœ… 4. JSON Validator

πŸ”Ž Goal:

Tell devs instantly if their JSON is valid or broken.

Built using try...catch around JSON.parse() with detailed error output:

try {
  JSON.parse(input);
  // success
} catch (e) {
  // show line number & error
}
Enter fullscreen mode Exit fullscreen mode

For extra polish:

  • I highlight the offending line
  • Offer a "fix suggestion" if possible

πŸ” Why it’s 100% Client-Side

All of these run fully in-browser. No tracking, no requests, no backend.

That means:

  • πŸ” Total data privacy
  • ⚑ Instant performance (no server delay)
  • βœ… Great for sensitive work or offline usage

πŸ’‘ Takeaways

Building this taught me a lot:

  • ✍️ JSON is simple but diffing it isn't
  • πŸ§ͺ RFC 6902 is strict β€” and unforgiving
  • βš™οΈ Client-side tools need to be fast, forgiving, and useful

πŸ”— Try it live

πŸ‘‰ https://ulidtool.net/#json-tools

Use it to:

  • Diff API responses
  • Patch config changes
  • Format messy JSON
  • Validate payloads quickly

And let me know:

  • What would make this better?
  • Want a CLI version? JSON Patch applier? Export to Git-style diff?

Thanks for reading!

If you use JSON in your workflow, this tool’s for you. Feedback is more than welcome πŸ™Œ

Top comments (0)