How to encode JSON to Base64
- Paste the JSON into Input · text, or press Sample to load an example object.
- Copy the Base64 from Output · Base64. The size figures show how much longer it got.
- For URLs, cookies or token-like values, tick URL-safe (- _) and No padding (=).
- Download saves the result as
base64.txt.
The sample on this page, {"id":42,"name":"base64hub","tags":["json","encode"],"active":true}, is 67 bytes and encodes to eyJpZCI6NDIsIm5hbWUiOiJiYXNlNjRodWIiLCJ0YWdzIjpbImpzb24iLCJlbmNvZGUiXSwiYWN0aXZlIjp0cnVlfQ==. To check a result, press Swap, or use Base64 to JSON, which also pretty-prints.
Whitespace and formatting change the Base64
Base64 encodes the exact characters of the JSON text, so two documents that mean the same thing produce different Base64 if a single space differs:
| JSON text | Base64 |
|---|---|
{"a":1} | eyJhIjoxfQ== |
{"a": 1} | eyJhIjogMX0= |
{ "a": 1 } | eyAiYSI6IDEgfQ== |
{"a":1} pretty-printed over three lines | ewogICJhIjogMQp9 |
When the Base64 is compared, cached or signed, produce the JSON with one serializer and fixed settings, and encode that exact string. Key order is another trap: most serializers keep insertion order, so {"a":1,"b":2} and {"b":2,"a":1} encode differently.
Python and JavaScript disagree by default
The object {"name": "Zoë", "n": 1} serialized by each language’s defaults:
| Serializer | JSON text | Base64 |
|---|---|---|
JavaScript JSON.stringify | {"name":"Zoë","n":1} | eyJuYW1lIjoiWm/DqyIsIm4iOjF9 |
Python json.dumps | {"name": "Zo\u00eb", "n": 1} | eyJuYW1lIjogIlpvXHUwMGViIiwgIm4iOiAxfQ== |
Python with separators=(",", ":"), ensure_ascii=False | {"name":"Zoë","n":1} | eyJuYW1lIjoiWm/DqyIsIm4iOjF9 |
With URL-safe and no padding ticked, the JavaScript version becomes eyJuYW1lIjoiWm_DqyIsIm4iOjF9.
Where Base64 JSON is used
- HTTP headers. Custom headers carrying context, such as tracing or user metadata, often hold Base64 JSON, since raw JSON quotes and non-ASCII characters are unsafe in header values. Many servers reject header lines longer than about 8 KB, so keep these payloads small.
- URL parameters. OAuth
statevalues, redirect targets and share links sometimes carry a small JSON object as Base64URL. - Environment variables and secrets. A whole JSON config fits in one variable without escaping, for example a service account key in a CI system.
- Token formats. JWT header and payload are Base64URL-encoded JSON without padding. To build a signed token, use the JWT encoder; this page only encodes.
Base64 adds a third to the size, and the JSON’s own quotes and keys are often larger than the values. Base64 size overhead shows when compressing first pays off.
JSON to Base64 in code
const obj = { name: "Zoë", n: 1 };
const bytes = new TextEncoder().encode(JSON.stringify(obj));
btoa(String.fromCharCode(...bytes)); // "eyJuYW1lIjoiWm/DqyIsIm4iOjF9"
Buffer.from(JSON.stringify(obj)).toString("base64url"); // "eyJuYW1lIjoiWm_DqyIsIm4iOjF9" (Node.js)
Without the TextEncoder step, btoa(JSON.stringify(obj)) encodes ë as a single Latin-1 byte (eyJuYW1lIjoiWm/rIiwibiI6MX0=), which UTF-8 decoders cannot read, and it throws on anything beyond Latin-1, such as an emoji.
import base64, json
obj = {"name": "Zoë", "n": 1}
text = json.dumps(obj, separators=(",", ":"), ensure_ascii=False)
base64.b64encode(text.encode("utf-8")).decode() # 'eyJuYW1lIjoiWm/DqyIsIm4iOjF9'
More options on the JavaScript, Node.js and Python pages.
FAQ
How do I convert JSON to Base64?
Serialize the JSON to a string, encode the string as UTF-8 and Base64-encode the bytes. Paste the JSON above for the result, or use btoa with a UTF-8 step in JavaScript and base64.b64encode(json.dumps(obj).encode()) in Python.
Why does the same object give different Base64 in Python and JavaScript?
Their serializers differ. Python’s json.dumps adds spaces after : and , and escapes non-ASCII as \u00eb, while JSON.stringify does neither. Use separators=(",", ":") and ensure_ascii=False to match.
Does this tool validate or minify my JSON?
No, it encodes the text exactly as you typed it, whitespace included. Minify the JSON first if the output has to be short or byte-identical to another system’s.
Should I use URL-safe Base64 for JSON in a URL?
Yes. Standard Base64 can contain +, / and =, which have meanings in URLs. Tick URL-safe and No padding, the same format JWT uses.