Base64 Encoding in JSON: A Developer's Guide

August 1, 20267 min read

JSON supports only a limited set of data types: strings, numbers, booleans, arrays, objects and null. Binary data — images, audio files, PDFs, cryptographic keys — has no native JSON representation. The solution is Base64 encoding: converting binary bytes into a string of safe ASCII characters that JSON can carry without corruption.

This guide explains Base64 thoroughly, covers the base64url variant used in JWT tokens, shows code examples in JavaScript and Python, and explains when Base64 is the right choice and when it is not.

How Base64 Works

Base64 encodes binary data as a sequence of characters from a 64-character alphabet: A–Z (26), a–z (26), 0–9 (10), + and / (2). Every 3 bytes of binary data become 4 Base64 characters. If the input is not a multiple of 3 bytes, = padding characters are added.

Example: the ASCII string Hi! (3 bytes: 0x48 0x69 0x21) encodes to SGkh in Base64.

base64url — The URL-Safe Variant

Standard Base64 uses + and /, which have special meaning in URLs (+ is a space, / is a path separator). Base64url replaces them:

JWT tokens use base64url for their header and payload sections. This is why JWT tokens look like eyJhbGci... and can be safely embedded in URLs and HTTP headers without URL encoding.

Common Uses of Base64 in JSON APIs

Base64 in JavaScript

// Encode a string to Base64 (browser and Node.js 16+)
const text = 'Hello, JSON!'
const encoded = btoa(text)
console.log(encoded) // 'SGVsbG8sIEpTT04h'

// Decode Base64 back to string
const decoded = atob(encoded)
console.log(decoded) // 'Hello, JSON!'

// UTF-8 support (btoa only handles Latin-1 by default)
function encodeUtf8(str) {
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_, p1) =>
    String.fromCharCode(parseInt(p1, 16))
  ))
}

function decodeUtf8(b64) {
  return decodeURIComponent(Array.from(atob(b64))
    .map(c => '%' + c.charCodeAt(0).toString(16).padStart(2, '0'))
    .join(''))
}

// In Node.js — use Buffer for simpler UTF-8 handling
const buf = Buffer.from('Hello, JSON! 🎉')
console.log(buf.toString('base64'))
console.log(Buffer.from(encoded, 'base64').toString('utf8'))

// Decode a JWT payload (base64url)
function decodeJwtPayload(token) {
  const payloadB64 = token.split('.')[1]
  const padded = payloadB64 + '=='.slice(0, (4 - payloadB64.length % 4) % 4)
  return JSON.parse(atob(padded.replace(/-/g, '+').replace(/_/g, '/')))
}

Base64 in Python

import base64

# Encode
text = 'Hello, JSON!'
encoded = base64.b64encode(text.encode('utf-8')).decode('ascii')
print(encoded)  # 'SGVsbG8sIEpTT04h'

# Decode
decoded = base64.b64decode(encoded).decode('utf-8')
print(decoded)  # 'Hello, JSON!'

# base64url (for JWT)
import base64

def b64url_decode(b64url):
    padding = 4 - len(b64url) % 4
    b64url += '=' * (padding % 4)
    return base64.urlsafe_b64decode(b64url)

# Decode a JWT payload
import json
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMifQ.abc'
payload = json.loads(b64url_decode(token.split('.')[1]))
print(payload)  # {'sub': '123'}

Try the IRJSON Base64 Tool

Base64 Size Overhead

Base64 encoding increases data size by approximately 33%. Every 3 bytes of binary data become 4 Base64 characters (plus padding). A 1 MB image embedded in JSON as Base64 becomes approximately 1.37 MB of Base64 text. Add the JSON overhead and the total payload is significantly larger than sending the file as binary.

Original SizeBase64 SizeOverhead
1 KB1.37 KB+37%
100 KB137 KB+37%
1 MB1.37 MB+37%
10 MB13.7 MB+37%

With gzip compression, Base64-encoded text compresses well and the effective overhead drops significantly. Still, for large binary payloads, multipart/form-data or binary streaming is more efficient than JSON + Base64.

Frequently Asked Questions

Is Base64 the same as encryption?

No. Base64 is an encoding scheme, not encryption. It converts binary data to printable ASCII for safe transmission but provides zero security — anyone can decode it instantly. Never use Base64 as a substitute for encryption.

What is the difference between base64 and base64url?

Standard Base64 uses + and / which are unsafe in URLs. Base64url replaces them with - and _ and removes = padding. JWT tokens use base64url so they can be safely placed in URLs, cookies and HTTP headers.

When should I use binary format instead of Base64?

Use binary format (multipart/form-data or application/octet-stream) for files larger than a few hundred KB. Base64 adds ~37% size overhead plus encoding and decoding CPU cost. For small blobs, or when the API contract requires JSON, Base64 is the right tool.

Encode and decode Base64 online free

IRJSON's Base64 tool uses only your browser's built-in atob/btoa functions. Your data never leaves your device.

Open Base64 Tool →