JWT Structure and Encoding
You'll learn how JWTs are structured into three parts and why base64url encoding makes them portable across web systems.
What is a JWT?
A JSON Web Token (JWT) is a compact, URL-safe way to represent claims between two parties. Think of it as a signed digital envelope: the contents are visible but tamper-proof, allowing anyone to read the data but only trusted parties to modify it.
The Three-Part Structure
A JWT consists of three distinct components separated by dots (.):
header.payload.signature
Header
The header describes the token type and signing algorithm. It's a JSON object like:
{"alg": "HS256", "typ": "JWT"}
Payload
The payload contains claims — statements about an entity (usually the user) and additional metadata. Common claims include sub (subject/user ID), exp (expiration time), and iat (issued at):
{"sub": "user123", "exp": 1735689600, "name": "Alice"}
Signature
The signature ensures integrity. It's created by encoding the header and payload, then signing them with a secret key using the algorithm specified in the header.
Base64url Encoding
Each component is base64url encoded — a URL-safe variant of base64 that replaces + with -, / with _, and removes padding (=). This makes JWTs safe to transmit in URLs, cookies, and HTTP headers without escaping special characters.
Example JWT:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyMTIzIiwiZXhwIjoxNzM1Njg5NjAwfQ.signature_here
The encoding is not encryption — anyone can decode and read the payload. The signature prevents tampering, not disclosure.
Key Takeaway: JWTs use three base64url-encoded components (header.payload.signature) to create portable, verifiable tokens where claims are readable but integrity-protected through cryptographic signing.