DEV Community

Sanjaya Sahu
Sanjaya Sahu

Posted on

Base64 and JSON Gotchas That Waste Hours (and How to Avoid Them)

If you work with APIs, you decode Base64 and format JSON daily. Here are the gotchas that cost me the most time, and how to avoid them.

1. Base64 is not encryption

Anyone can decode it. If you see credentials in a Base64 string (HTTP Basic auth headers, Kubernetes secrets), treat them as plaintext. echo -n 'user:pass' | base64 is encoding, nothing more.

2. URL-safe Base64 breaks naive decoders

JWTs use Base64URL: - and _ instead of + and /, with padding stripped. Feeding that to a standard decoder fails. Convert first:

const b64 = s.replace(/-/g, '+').replace(/_/g, '/');
const padded = b64 + '='.repeat((4 - (b64.length % 4)) % 4);
Enter fullscreen mode Exit fullscreen mode

3. Unicode and btoa()

btoa('héllo') throws in browsers because it only handles Latin-1. Encode to UTF-8 bytes first with TextEncoder, then Base64 the bytes.

4. JSON that "looks fine" but isn't valid

Trailing commas, single quotes, comments and unquoted keys are valid JavaScript but invalid JSON. A formatter that points to the exact line and column saves minutes of squinting.

5. Big numbers silently lose precision

JSON.parse('{"id": 9007199254740993}') gives you 9007199254740992. IDs above 2^53 should travel as strings.

6. Don't paste production secrets into random websites

Many online formatters send your input to a server. Use one that runs fully in your browser and check the Network tab to confirm.

Tools

I built free client-side versions of these (no upload, no signup):

Disclosure: I'm the author of Web Developer Toolbox. Which encoding gotcha got you?

Top comments (0)