Decode a JWT in JavaScript, Python and the Command Line
A JWT (JSON Web Token) looks like random text, but two of its three parts are just Base64URL-encoded JSON. This guide shows how to decode a JWT in JavaScript (browser and Node.js), Python and on the command line, with and without a library, plus how to check if it has expired and how to verify it properly on a server. Every example was run against the sample token below.
The sample token
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzQyIiwibmFtZSI6IkFubiBMZWUiLCJyb2xlIjoiYWRtaW4iLCJpYXQiOjE3NjcyMjU2MDAsImV4cCI6MTc2NzIyOTIwMH0.ZQZ3O4FcTeCkStdKG1vzOFSrndZBR8XBy5WY2c8Ip-IThe three parts, separated by dots, are the header, the payload and the signature. Decoded:
// header
{ "alg": "HS256", "typ": "JWT" }
// payload
{
"sub": "user_42",
"name": "Ann Lee",
"role": "admin",
"iat": 1767225600,
"exp": 1767229200
}"role": "admin". Decoding just reads it. Only verifying the signature with the secret or public key proves the token is real. Decode on the client to show things; verify on the server before you trust anything.JavaScript: with the jwt-decode library
npm install jwt-decodeimport { jwtDecode } from 'jwt-decode'
const payload = jwtDecode(token)
// { sub: 'user_42', name: 'Ann Lee', role: 'admin', iat: 1767225600, exp: 1767229200 }
const header = jwtDecode(token, { header: true })
// { alg: 'HS256', typ: 'JWT' }Since version 4, the import is { jwtDecode } with curly braces. Old tutorials use import jwt_decode from 'jwt-decode', which now fails with "does not provide an export named default". An invalid token throws InvalidTokenError, so wrap the call in try/catch.
JavaScript: without a library
Decoding is only a few lines, so you may not need a dependency at all.
Node.js
const payload = JSON.parse(
Buffer.from(token.split('.')[1], 'base64url').toString('utf8')
)Browser
function decodeJwt(token) {
const part = token.split('.')[1]
const base64 = part.replace(/-/g, '+').replace(/_/g, '/')
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0))
return JSON.parse(new TextDecoder().decode(bytes))
}The replace calls turn Base64URL into normal Base64, and TextDecoder makes names with accents or non-Latin letters come out right. A plain JSON.parse(atob(part)) works for simple tokens but breaks on those. Details in Base64 explained.
Check if the token has expired
exp is a Unix timestamp in seconds. JavaScript dates use milliseconds, so multiply by 1000:
const { exp } = jwtDecode(token)
const expiresAt = new Date(exp * 1000) // 2026-01-01T01:00:00.000Z
const isExpired = Date.now() >= exp * 1000
// Refresh a little early to allow for clock differences
const expiresSoon = Date.now() >= (exp - 60) * 1000* 1000 is a classic bug: the date comes out as January 1970 and every token looks expired. See Dates in JSON for more.Python: PyJWT
pip install PyJWTDecode only (read the claims, no checks)
import jwt
payload = jwt.decode(token, options={"verify_signature": False})
# {'sub': 'user_42', 'name': 'Ann Lee', 'role': 'admin', 'iat': 1767225600, 'exp': 1767229200}
header = jwt.get_unverified_header(token)
# {'alg': 'HS256', 'typ': 'JWT'}Verify (on the server)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
except jwt.ExpiredSignatureError:
... # "Signature has expired": ask the user to log in again
except jwt.InvalidTokenError:
... # wrong signature, bad format, etc.: reject the requestjwt.decode with a key checks the signature and the exp time. Always pass algorithms=[...] with the exact algorithm you expect; never read it from the token's own header.
Without a library
import base64, json
part = token.split(".")[1]
part += "=" * (-len(part) % 4) # add the padding JWTs leave out
payload = json.loads(base64.urlsafe_b64decode(part))Without the padding line you get binascii.Error: Incorrect padding for most tokens.
Command line
# with jq
echo "$TOKEN" | cut -d. -f2 | jq -R 'gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'
# with a header too
echo "$TOKEN" | jq -R 'split(".") | .[0:2] | map(gsub("-";"+") | gsub("_";"/") | @base64d | fromjson)'Avoid pasting production tokens into random websites or shared terminals: a valid token is like a password until it expires. The JWT Decoder on this site runs entirely in your browser, but the safest choice for live tokens is local code like the examples above.
Verify in Node.js
For verifying on a Node.js server, use a library such as jose (works everywhere, including edge runtimes) or jsonwebtoken:
import { jwtVerify } from 'jose'
const secret = new TextEncoder().encode(process.env.JWT_SECRET)
const { payload } = await jwtVerify(token, secret, { algorithms: ['HS256'] })
// throws if the signature is wrong or the token has expiredCommon errors
| Error | Cause |
|---|---|
Invalid token specified: missing part #2 | Not a JWT, or only part of it was copied. A JWT has exactly two dots. |
Incorrect padding (Python) | Base64URL without the = padding. Add it, or use PyJWT. |
Unexpected token in JSON | You decoded with plain Base64, or included "Bearer " at the start. |
Signature has expired | exp is in the past. Get a new token (refresh or log in again). |
Signature verification failed | Wrong secret or key, or the token was changed. |
To learn what each claim means (sub, aud, iss, nbf and more), read How to decode a JWT and read its claims.