JWT Decoder — Developer Code Samples

JWT (JSON Web Tokens) consist of three Base64url-encoded parts: header, payload, and signature. Decoding the header and payload requires no secret key. Verifying the signature requires the secret or public key. Use PyJWT in Python or the jsonwebtoken package in Node.js.

Try the interactive version online: Open JWT Decoder Tool →

Parameters

Parameter Type Required Description
token str Yes JWT token string (three Base64url parts separated by dots)
verify bool No Verify signature (requires secret/public key, default: False)
secret str No Secret key or public key for signature verification

Returns: Dict with header (algorithm/type), payload (claims including sub, iat, exp), and signature fields

Code Examples

import base64
import json
import hmac
import hashlib

def decode_jwt(token, verify=False, secret=None):
    """
    Decode a JWT token without verification (inspect only).
    For production: use PyJWT with signature verification.
    """
    parts = token.split('.')
    if len(parts) != 3:
        raise ValueError("Invalid JWT: must have 3 parts separated by dots")

    def decode_part(part):
        # Add padding if needed
        padding = 4 - len(part) % 4
        if padding != 4:
            part += '=' * padding
        decoded = base64.urlsafe_b64decode(part)
        return json.loads(decoded)

    header = decode_part(parts[0])
    payload = decode_part(parts[1])
    signature = parts[2]

    return {
        "header": header,
        "payload": payload,
        "signature": signature,
        "is_expired": False,  # Check payload['exp'] vs time.time()
    }

# Example JWT (never use in production without verification!)
sample_jwt = (
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
    ".eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFsaWNlIiwiaWF0IjoxNTE2MjM5MDIyfQ"
    ".SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
)

result = decode_jwt(sample_jwt)
print("Header:", json.dumps(result["header"], indent=2))
print("Payload:", json.dumps(result["payload"], indent=2))

# With PyJWT (pip install PyJWT) - includes verification
# import jwt
# decoded = jwt.decode(token, "your-secret", algorithms=["HS256"])
# print(decoded)

# Check expiration
import time
payload = result["payload"]
if "exp" in payload:
    is_expired = payload["exp"] < time.time()
    print(f"Token expired: {is_expired}")