How to decode a JWT (and why decoding is not verifying)
A login request fails with 401 Unauthorized, and the only clue is a long string starting with eyJ in the Authorization header. That string is a JSON Web Token (JWT), and you can read most of it without any key at all. This guide shows how a JWT is built, how to decode it by hand and in code, how to read its timestamps — and why being able to read a token tells you nothing about whether you should trust it.
The structure: header.payload.signature
A JWT is three pieces of text joined by dots. The first two are JSON objects encoded with Base64url — the URL-safe flavour of Base64 that uses - and _ instead of + and / and drops the = padding. The third piece is the signature, also Base64url, but it is raw bytes, not JSON.
| Part | Contains | Readable? |
|---|---|---|
| Header | The algorithm (alg), the type (typ), sometimes a key ID (kid) | Yes — just decode it |
| Payload | The claims: who the user is, who issued the token, when it expires | Yes — just decode it |
| Signature | A MAC or digital signature over header.payload | No — only checkable with the right key |
Why does every JWT start with eyJ? Because the header always begins with {", and {" in Base64 is eyJ.
A worked example
Here is a demo token we generated for this guide. It is signed with the throw-away secret demo-secret-not-for-production and describes a fictional user, so it is safe to paste anywhere.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJzdWIiOiJ1c2VyXzEwMjQiLCJhdWQiOiJhcGkuZXhhbXBsZS5jb20iLCJuYW1lIjoiRGVtbyBVc2VyIiwicm9sZSI6ImVkaXRvciIsImlhdCI6MTc5MDQ2NzIwMCwiZXhwIjoxNzkwNDcwODAwfQ.wn-vHA442i0OIgh1K406fNWRgxPgNev3VDMf2NZyzxg
Split it at the dots and decode the first two parts:
// header
{ "alg": "HS256", "typ": "JWT" }
// payload
{
"iss": "https://auth.example.com",
"sub": "user_1024",
"aud": "api.example.com",
"name": "Demo User",
"role": "editor",
"iat": 1790467200,
"exp": 1790470800
}
The third part, wn-vHA44…, decodes to 32 bytes: the output of HMAC-SHA256. It looks like noise because it is.
Registered claims and how to read them
RFC 7519 defines seven short claim names. None is mandatory, but most APIs use several of them:
| Claim | Meaning | In our token |
|---|---|---|
iss | Issuer — who created the token | https://auth.example.com |
sub | Subject — usually the user ID | user_1024 |
aud | Audience — which API should accept it (string or array) | api.example.com |
exp | Expiration time | 1790470800 |
nbf | Not before — invalid until this time | not present |
iat | Issued at | 1790467200 |
jti | JWT ID — a unique ID, useful for blocklists and replay detection | not present |
Everything else (name, role) is a private claim that the issuer made up.
The time claims are Unix timestamps in seconds since 1 January 1970 UTC — not milliseconds. JavaScript’s Date works in milliseconds, so multiply by 1000:
new Date(1790470800 * 1000).toISOString()
// '2026-09-27T01:00:00.000Z' → 7:00 AM in Dhaka (UTC+6)
So this token was issued at 00:00 UTC and was valid for exactly one hour (exp − iat = 3600). Validators normally allow a small clock skew of a minute or so.
Common bug: comparing exp with Date.now() directly. Date.now() is in milliseconds, so every token looks expired since 1970. Compare with Date.now() / 1000.
Decoding in code
JavaScript (browser or Node)
function decodePart(part) {
const b64 = part.replace(/-/g, '+').replace(/_/g, '/');
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
return JSON.parse(new TextDecoder().decode(bytes)); // UTF-8 safe
}
const [header, payload] = token.split('.').map(decodePart);
console.log(payload.exp < Date.now() / 1000); // true = expired
The TextDecoder step matters: atob returns one character per byte, so names in Bangla or with accents would come out garbled without it. In Node you can simply write Buffer.from(part, 'base64url').
Python
import base64, json
def b64url_decode(part):
part += "=" * (-len(part) % 4) # restore the padding
return base64.urlsafe_b64decode(part)
header_b64, payload_b64, signature_b64 = token.split(".")
print(json.loads(b64url_decode(payload_b64)))
Command line
node -e "console.log(JSON.parse(Buffer.from(process.argv[1].split('.')[1], 'base64url')))" "$TOKEN"
Plain base64 -d often fails on JWTs because of the missing padding and the -/_ characters; convert them first or use a one-liner like the one above.
Decoding is not verifying
Everything above needed no secret. That is the point: anyone can decode a JWT, and anyone can create one. An attacker can change "role": "editor" to "admin", re-encode the payload and send it. The only thing that exposes the forgery is the signature, because it no longer matches. A server must verify the signature — and then check exp, nbf, iss and aud — before trusting a single claim.
Never make authorization decisions from a decoded-but-unverified payload on the server. Decoding in the front end to show a name or an expiry countdown is fine; the back end must still verify.
How verification works: HS256 vs RS256/ES256
HS256 (HMAC with SHA-256) uses one shared secret. The issuer computes HMAC(secret, header + "." + payload); the verifier recomputes it with the same secret and compares. Simple and fast, but every service that can verify can also mint tokens, so the secret must stay on trusted servers.
import { createHmac, timingSafeEqual } from 'node:crypto';
function verifyHS256(token, secret) {
const [h, p, s] = token.split('.');
const header = JSON.parse(Buffer.from(h, 'base64url'));
if (header.alg !== 'HS256') throw new Error('unexpected alg');
const expected = createHmac('sha256', secret).update(`${h}.${p}`).digest();
const given = Buffer.from(s, 'base64url');
if (given.length !== expected.length || !timingSafeEqual(given, expected)) {
throw new Error('bad signature');
}
return JSON.parse(Buffer.from(p, 'base64url'));
}
That sketch shows the mechanism only — it still skips the exp/aud/iss checks. In production use a maintained library such as jose (JavaScript) or PyJWT (Python) and pass the allowed algorithms explicitly.
RS256 (RSA) and ES256 (ECDSA on the P-256 curve) are asymmetric. The issuer signs with a private key it never shares; anyone verifies with the matching public key. Identity providers publish their public keys as a JWKS (JSON Web Key Set), usually at a URL such as /.well-known/jwks.json. The token’s kid header says which key in the set to use, which lets the provider rotate keys without breaking clients. Use asymmetric algorithms whenever the verifier is a different party from the issuer.
What about JWE? A signed JWT (JWS) is readable by anyone; a JSON Web Encryption token actually encrypts the payload. You can recognise one because it has five dot-separated parts instead of three, and the payload cannot be decoded without the key.
exp into a readable date and checks HS, RS, PS and ES signatures right in your browser — the token never leaves your device.
Try it now
Next, read 8 JWT security mistakes developers still make to see what goes wrong when verification is done carelessly.
FAQ
Can I decode a JWT without the secret key?
Yes. The header and payload are only Base64url-encoded, not encrypted. The key is needed to verify the signature (or to create a new valid token), not to read the claims.
Why does my decoder say the token is expired when it just arrived?
Check the units first: exp is in seconds. Then check the server and client clocks — a machine that is a few minutes off will reject fresh tokens. Most libraries accept a small leeway setting for this.
Is it safe to paste a real token into an online decoder?
Only if decoding happens locally. A valid access token is a password for as long as it lives. JSON Bondhu decodes and verifies in your browser, but for production tokens the safest habit is to use a demo token or wait until it expires.
What is the difference between sub and jti?
sub identifies the user (the same for every token that user gets); jti identifies this one token, so a server can revoke it or detect it being replayed.