JSON Base64 Encode/Decode

Encode JSON to Base64 or decode Base64 strings back to JSON:

What is Base64?

Base64 is a binary-to-text encoding scheme that converts binary data into ASCII characters. It's commonly used to embed binary data in text-based formats like JSON, XML, or HTML.

Why Base64 Encode JSON?

Common use cases for Base64-encoded JSON:

  • API Authentication — Many APIs expect credentials as Base64-encoded JSON
  • JWT Tokens — JWT payloads are Base64-encoded JSON
  • Data URIs — Embedding data in URLs or HTML
  • Cookie Storage — Storing complex data in cookies
  • Query Parameters — Passing JSON through URLs safely

Example

Original JSON:

{"name": "John", "role": "admin"}

Base64 encoded:

eyJuYW1lIjogIkpvaG4iLCAicm9sZSI6ICJhZG1pbiJ9

Base64 in Code

JavaScript

// Encode JSON to Base64
const json = { name: "John", role: "admin" };
const base64 = btoa(JSON.stringify(json));

// Decode Base64 to JSON
const decoded = JSON.parse(atob(base64));

Python

import base64
import json

# Encode
data = {"name": "John", "role": "admin"}
encoded = base64.b64encode(json.dumps(data).encode()).decode()

# Decode
decoded = json.loads(base64.b64decode(encoded).decode())

Command Line

# Encode
echo '{"name":"John"}' | base64

# Decode
echo 'eyJuYW1lIjoiSm9obiJ9' | base64 -d

Base64 URL-Safe Encoding

Standard Base64 uses + and / characters, which have special meaning in URLs. URL-safe Base64 replaces these with - and _.

StandardURL-Safe
+-
/_
= (padding)Often omitted

Common Mistakes

  • Encoding twice — If your JSON is already Base64, encoding again creates double-encoding issues
  • Character encoding — Always use UTF-8 when encoding strings with non-ASCII characters
  • Padding issues — Some systems strip = padding, which can cause decode errors

Related Tools