JSON to Base64
Instantly encode any JSON object as Base64 or Base64URL. Supports pretty-print and minify, auto-generates browser and Node.js code examples — ideal for JWT payloads, API transport, and URL parameters.
Output
Typical Use Cases
JWT Payload
The JWT Header and Payload are both Base64URL-encoded JSON. This tool quickly generates the Base64URL representation of a payload for debugging or manually constructing tokens.
{"sub":"1234567890","name":"John","iat":1516239022}API Transport
Encode complex JSON configs or file metadata as a single Base64 string for URL parameters, HTTP headers, or simple fields — avoiding parsing issues from special characters and nested structures.
{"action":"upload","type":"image/png","size":204800}State Persistence
Encode page state, filter settings, or user preferences as Base64URL and put it in the URL hash or localStorage — enabling shareable URLs and cross-session state restoration.
{"page":2,"sort":"date","filters":{"type":"pdf"}}Encoding Process
- 1
Parse JSON
Use JSON.parse() to validate the input and ensure it is a valid JSON object or array.
- 2
Serialize
Use JSON.stringify() to convert the object back to a string. Choose minified (no whitespace) or pretty-printed (with indentation).
- 3
UTF-8 → bytes
Use TextEncoder to convert the JSON string to a UTF-8 byte sequence, correctly handling Chinese characters and emoji.
- 4
Base64 encoding
Convert the byte sequence to a Base64 string (standard), or further to Base64URL (replace + / and remove padding).
Implementation Comparison
JavaScript (Browser)
btoa(JSON.stringify(data))JavaScript (Base64URL)
btoa(JSON.stringify(data))
.replace(/\+/g,'-').replace(/\//g,'_').replace(/=+$/,'')Node.js
Buffer.from(JSON.stringify(data)).toString('base64')Python
import base64, json
base64.b64encode(json.dumps(data).encode()).decode()Go
import "encoding/base64"
import "encoding/json"
b, _ := json.Marshal(data)
base64.StdEncoding.EncodeToString(b)Frequently Asked Questions
Why encode JSON as Base64?
Base64 converts JSON into a pure ASCII string, suitable for text protocols that do not support binary. Common cases include putting JSON in HTTP headers, URL parameters, cookies, JWT payloads, and embedding it in XML or CSV files.
What is the difference between minified and pretty-printed encoding?
Minifying removes whitespace and newlines from JSON, producing shorter Base64 output. Pretty-printing preserves indentation, producing longer output but keeping the raw data readable. Minified is recommended for most API transport; pretty-printed is useful for debugging or documentation.
What is the difference between Base64URL and Base64, and which does JWT use?
Base64URL replaces + with -, / with _, and removes trailing = padding, making it safe for URLs and HTTP headers without extra encoding. JWT (JSON Web Token) Headers and Payloads use Base64URL encoding, not standard Base64.
Can the encoded Base64 string be decoded back?
Yes, Base64 encoding is reversible. Use atob(encoded) to recover the Base64 string, then JSON.parse() to parse it back into a JSON object. Base64 is not an encryption algorithm — anyone can decode the content, so do not use it to protect sensitive data.
How do I encode JSON as Base64 in Python?
import base64, json; encoded = base64.b64encode(json.dumps(data).encode()).decode(). To decode: data = json.loads(base64.b64decode(encoded).decode()).
Is encoding correct when JSON contains Chinese or emoji?
The tool uses TextEncoder to convert the JSON string to UTF-8 bytes before Base64 encoding, correctly handling Chinese, Japanese, emoji, and other Unicode characters. Plain btoa() fails on non-ASCII characters; this tool handles that compatibility.