How to Decode a JWT Safely: Every Claim Explained
Read any JSON Web Token: what the header, payload and standard claims mean, why decoding isn’t verifying, and seven mistakes that cause breaches.
If you build anything with a login, you will meet JSON Web Tokens: in an Authorization: Bearer … header, in a cookie, in the URL after an OAuth redirect. They look like gibberish, but a JWT is not encrypted. It is three pieces of Base64URL-encoded text that anyone holding the token can read.
That is useful when you're debugging, and dangerous when you forget it. This guide shows how to read a token, what every standard claim means, why decoding is not the same as verifying, and the mistakes that turn JWTs into security holes.
What a JWT looks like
Here is a real (signed, harmless) token. The dots split it into three parts:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.eyJzdWIiOiJ1c2VyXzhmM2EiLCJuYW1lIjoiUnl1amluIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNzkzMjMyMDAwLCJleHAiOjE3OTMyMzU2MDB9
.s61GkeieHLkbCqEapudPOdrpSXORBG-1kKMrZDu-MhE
- Header: which algorithm signed the token, and its type.
- Payload: the claims, the data the token carries.
- Signature: proof that the first two parts haven't been changed since the token was issued.
Decode the first two parts and you get plain JSON:
{ "alg": "HS256", "typ": "JWT" }
{
"sub": "user_8f3a",
"name": "Ryujin",
"role": "admin",
"iat": 1793232000,
"exp": 1793235600
}
Paste the token into the JWT decoder and it does this for you, explains each claim, and tells you whether the token has expired. It runs entirely in your browser, which matters for reasons we'll get to.
Base64URL, not Base64
Each part is encoded with Base64URL, a variant of Base64 that is safe in URLs and headers: + becomes -, / becomes _, and the = padding at the end is dropped. That's why a plain base64 -d often fails on a JWT.
To decode a part in code:
// Browser or Node.js: read a JWT's payload (no verification!)
function readPayload(token) {
const part = token.split('.')[1];
const base64 = part.replace(/-/g, '+').replace(/_/g, '/');
const bytes = Uint8Array.from(atob(base64), (char) => char.charCodeAt(0));
return JSON.parse(new TextDecoder().decode(bytes));
}
atob gives you bytes, not text, so the TextDecoder step is what makes names with accents or emoji come out right. In Node.js you can skip the dance: Buffer.from(part, 'base64url').toString('utf8').
In Python:
import base64, json
def read_payload(token: str) -> dict:
part = token.split(".")[1]
part += "=" * (-len(part) % 4) # restore the padding Base64URL drops
return json.loads(base64.urlsafe_b64decode(part))
If you just want to look inside one part, the Base64 tool has a Base64URL mode, and the JSON formatter will pretty-print the result.
What every claim means
The JWT standard (RFC 7519) registers seven claim names. None of them is required, but these are the ones libraries understand:
| Claim | Name | What it means |
|---|---|---|
iss | Issuer | Who created and signed the token, often your auth server's URL. |
sub | Subject | Who the token is about, usually a user ID. Unique within the issuer. |
aud | Audience | Who the token is for: the API that should accept it. A string or a list. |
exp | Expiration time | After this moment the token must be rejected. |
nbf | Not before | Before this moment the token must be rejected. |
iat | Issued at | When the token was created. |
jti | JWT ID | A unique ID for this token, handy for blocking one token or preventing replay. |
Everything else (name, role, email, scope…) is a private or public claim your system defines. Providers such as Auth0, Firebase or Cognito add their own, so check their docs before you trust a field.
Times are seconds, not milliseconds
exp, nbf and iat are NumericDate values: seconds since 1 January 1970 UTC. JavaScript's Date.now() returns milliseconds, which is the root of a classic bug where tokens "expire" in the year 58,000:
const now = Math.floor(Date.now() / 1000); // seconds, like the claims
const expired = payload.exp <= now;
In the example above, iat is 1793232000 and exp is 1793235600: issued at 00:00 UTC on 29 October 2026 and valid for one hour. The timestamp converter turns these into dates in your time zone.
The header: alg, typ and kid
The header usually holds three fields:
alg: the signing algorithm.HS256is HMAC with SHA-256, using one shared secret to sign and verify.RS256(RSA) andES256(ECDSA) use a private key to sign and a public key to verify, so many services can check tokens without being able to mint them.EdDSAis a newer, compact option.typ: the token type, usuallyJWT.kid: a key ID, telling the verifier which of the issuer's keys was used. Providers publish their public keys as a JSON Web Key Set (JWKS), andkidpicks one.
Decoding is not verifying
This is the part that matters most. Decoding reads the token. Verifying proves it's genuine and still valid:
- Check the signature with the right key, using an algorithm you chose in advance.
- Check
expandnbfagainst the current time, with a small allowance for clock drift. - Check
issandaud, so a token from another issuer, or meant for another API, is refused.
Anyone can create a token whose payload says "role": "admin". Only the signature tells you whether your issuer actually said so. Here is verification with the widely used jose library for Node.js and browsers:
import { createRemoteJWKSet, jwtVerify } from 'jose';
const keys = createRemoteJWKSet(new URL('https://auth.example.com/.well-known/jwks.json'));
export async function verify(token) {
const { payload } = await jwtVerify(token, keys, {
issuer: 'https://auth.example.com/',
audience: 'orders-api',
algorithms: ['RS256'], // never let the token choose
});
return payload; // only now can you trust payload.sub and payload.role
}
jwtVerify throws if the signature, the times, the issuer or the audience don't check out, so the happy path is the only one that returns a payload.
Seven mistakes that cause breaches
- Trusting a decoded payload. Reading
rolefrom a token you haven't verified is the same as reading it from the request body. Verify on the server, every time. - Accepting
alg: none. The standard allows unsigned tokens. A verifier that honours whateveralgthe header asks for can be handed an unsigned token. Pin the algorithms you expect, as in the example above. - Algorithm confusion. If a server signs with RS256 but its library also accepts HS256, an attacker can sign a token with HMAC, using the server's public key as the shared secret. Pinning algorithms closes this hole too.
- Weak HMAC secrets. An HS256 token is only as strong as its secret. Anyone with one token can try to guess the secret offline, at billions of guesses a second. Use at least 256 random bits: in the password generator, choose hex and 64 characters.
- Secrets in the payload. The payload is readable by anyone who holds the token: the user, their browser extensions, your logs. Keep it to identifiers and permissions. If you really need hidden contents, use an encrypted token (JWE).
- Tokens that live for weeks. A stolen JWT works until it expires, and you can't easily revoke it. Keep access tokens short (minutes to an hour), and use refresh tokens you can revoke on the server.
- Not checking
audandiss. If several apps share an identity provider, a token issued for one may be signed by the same key as tokens for the others. Without an audience check, it works against your API too.
Where to store a JWT in the browser
There's no perfect answer, only trade-offs:
localStorageis easy to use, but any script on your page can read it. One cross-site scripting (XSS) bug, or one compromised npm package, and the token is gone.- An
HttpOnly,Secure,SameSitecookie can't be read by JavaScript, which removes that risk. In exchange, you need to think about cross-site request forgery (CSRF).SameSite=LaxorStricthandles most of it.
For most web apps, an HttpOnly cookie set by your own backend is the safer default.
Is it safe to paste a token into a decoder?
A valid access token is a credential: whoever holds it is you, until it expires. Before pasting a real token anywhere:
- Use a decoder that works locally. The DevJiaVu JWT decoder runs in your browser and never puts the token in a URL. Even when you paste a token into the toolkit's search, it's passed to the decoder in memory, not through the address bar.
- Prefer tokens from a test environment, or ones that have already expired.
- Never paste production tokens into a chat, a ticket or a shared document.
Summary
- A JWT is a header, a payload and a signature, each Base64URL-encoded. Anyone with the token can read the first two.
exp,nbfandiatare in seconds.iss,sub,audandjtiidentify who issued the token, who it's about and who it's for.- Decoding is for debugging. Trust comes only from verifying the signature, with pinned algorithms, plus the expiry, issuer and audience.
Keep the JWT decoder bookmarked for the next time an API answers 401 Unauthorized and you need to see what's really inside the token.