Dev.to Security 🔐 Cybersecurity 👁 0 📖 5 min read

How to Decode a JWT Without Pasting It Into a Random Website

You're debugging a login bug. The API returns 401, and all you have is a long string that starts with eyJ. The fastest move is to paste it into the first "JWT decoder" Google shows you. Before you do that, stop for a se

How to Decode a JWT Without Pasting It Into a Random Website

You're debugging a login bug. The API returns 401, and all you have is a long string that starts with eyJ. The fastest move is to paste it into the first "JWT decoder" Google shows you.

Before you do that, stop for a second. That string is a live credential. Whoever holds it can usually act as that user until it expires. Pasting a production token into a site you don't know is the same as pasting a password.

The good news: you don't need any website to read a JWT. It's two pieces of Base64URL-encoded JSON and a signature. You can decode it in one line of JavaScript, Node or Python, on your own machine.

This guide shows how to do it by hand, what the claims mean, and why "it decodes" never means "it's valid".

What a JWT actually is

A JSON Web Token (defined in RFC 7519) is three parts joined by dots:

header.payload.signature

Here's the demo token we'll use in this post. It was signed with the HS256 secret demo-secret-not-real, so it's safe to paste anywhere:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzg0MjEiLCJuYW1lIjoiQXNoYSBEZW1vIiwicm9sZSI6ImVkaXRvciIsImlhdCI6MTc5NTE2NTIwMCwiZXhwIjoxNzk1MTY4ODAwfQ.QIFnuakPNSLDZM2JAkJ6eikvtCzLnzSFBPkWbsNijD0

A JWT split into its three parts: header and payload decode to JSON, the signature can only be verified

  • Header: which algorithm signed the token (alg) and its type (typ).
  • Payload: the claims, such as who the user is and when the token expires.
  • Signature: a hash of the first two parts made with a secret or private key.

The header and payload are only encoded, not encrypted. Anyone who has the token can read them. That's also why you should never put passwords, API keys or personal data in a JWT payload.

Decode a JWT by hand

Base64URL is normal Base64 with small changes: + becomes -, / becomes _, and the = padding is dropped. So decoding is: swap the characters back, add the padding, then Base64-decode.

In the browser console (JavaScript)

function decodeJwt(token) {
  const [header, payload] = token.split('.').slice(0, 2).map((part) => {
    const b64 = part.replace(/-/g, '+').replace(/_/g, '/');
    const padded = b64 + '='.repeat((4 - (b64.length % 4)) % 4);
    const bytes = Uint8Array.from(atob(padded), (c) => c.charCodeAt(0));
    return JSON.parse(new TextDecoder().decode(bytes));
  });
  return { header, payload };
}

decodeJwt('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIi...');

The TextDecoder step matters. atob() returns raw bytes as a string, so a name like "José" or any emoji in the payload breaks without it.

In Node.js

Node understands Base64URL directly, so it's shorter:

const [h, p] = token.split('.');
console.log(JSON.parse(Buffer.from(h, 'base64url').toString()));
console.log(JSON.parse(Buffer.from(p, 'base64url').toString()));

In the terminal (Python)

The classic cut | base64 -d trick often fails with invalid input, because the padding is missing. Python handles it cleanly:

python3 -c "import sys,base64,json; p=sys.argv[1].split('.')[1]; print(json.dumps(json.loads(base64.urlsafe_b64decode(p+'='*(-len(p)%4))),indent=2))" "$TOKEN"

All three give you the same payload:

{
  "sub": "user_8421",
  "name": "Asha Demo",
  "role": "editor",
  "iat": 1795165200,
  "exp": 1795168800
}

Reading the claims

  • sub is the subject, usually a user ID.
  • iat (issued at) and exp (expires) are NumericDate values: seconds since 1 January 1970 UTC, not milliseconds. In JavaScript that means new Date(exp * 1000). Here, iat is 20 Nov 2026, 09:00 UTC and exp is exactly one hour later. If you need to check a date quickly, a Unix timestamp converter does it in a second.
  • Anything else, like role here, is a custom claim your issuer added.

A surprising number of "random 401" bugs are just an exp in the past, or a server clock that's a few minutes off.

Decoding is not verifying

Decoding vs verifying a JWT: decoding needs no key, verifying needs the secret or public key

This is the part that bites people. Decoding tells you what a token says. It doesn't tell you whether it's true. Anyone can build a token with "role": "admin" and encode it. Only the signature proves it came from your issuer and wasn't changed.

To verify, you need the key:

  • HS256 / HS384 / HS512: the same shared secret the issuer used.
  • RS256, PS256, ES256, EdDSA: the issuer's public key, often published as a JWKS (JSON Web Key Set) at a URL like /.well-known/jwks.json.

Your backend should verify with a proper library, and also check exp, nbf, iss and aud. And never accept a token whose header says "alg": "none".

The faster way: decode and verify in your browser

If you'd rather not keep a snippet around, we built a JWT decoder into MyToolStudio, our collection of free browser tools.

MyToolStudio JWT Decoder showing the demo token with Signature verified

Paste a token and it:

  • colours the header, payload and signature so you can see where each part starts,
  • shows the decoded JSON and a "Claims explained" table, with iat, nbf and exp turned into readable dates,
  • verifies the signature when you paste the secret (HS) or a public key, PEM certificate, JWK or JWKS (RS, PS, ES and EdDSA),
  • lets you build and sign test tokens in the "Encode and sign" tab.

Verification uses the browser's Web Crypto API, so the token and key are never sent anywhere. That's the whole point. It's free and there's no sign-up.

Common mistakes

  1. Treating a decoded token as trusted. Always verify the signature on the server.
  2. Using atob() alone. It breaks on non-ASCII characters and on missing padding.
  3. Reading exp as milliseconds. It's seconds. Date.now() / 1000 is the value to compare.
  4. Storing secrets in the payload. The payload is public to anyone holding the token.
  5. Pasting production tokens into unknown sites. Use a local snippet or a tool that runs fully in your browser.

FAQ

Can I decode a JWT without the secret?
Yes. The header and payload are Base64URL-encoded JSON, so no key is needed to read them. You only need the secret or public key to verify the signature.

Is it safe to paste a JWT into an online decoder?
Only if the tool runs in your browser and doesn't send the token to a server. If you're not sure, use one of the snippets above. Never paste a production token into a site you don't trust.

Why does my token decode but my API still rejects it?
Usually it's an expired exp, a wrong aud or iss, a clock difference between servers, or a signature made with a different key. Decoding shows the first three; verifying with the right key shows the last one.

More free developer tools we use every day: Base64 encoder and decoder, JSON formatter, and the full list of 300+ free tools on MyToolStudio. Further reading: RFC 7519 and MDN on Base64.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.