What does jwt decode do
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- JWTs are composed of three parts: header, payload, and signature, separated by dots.
- The header typically contains metadata about the token, such as the algorithm used.
- The payload contains the claims, which are statements about an entity (e.g., user ID, permissions).
- The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message was not changed along the way.
- Decoding involves base64 URL decoding the header and payload, and then signature verification.
What is a JWT?
A JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. It is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs are commonly used for authentication and authorization in web applications and APIs.
Structure of a JWT
A JWT consists of three parts, separated by dots ('.'):
- Header: The header is a JSON object that typically contains metadata about the token, such as the type of token (JWT) and the cryptographic algorithm used for signing (e.g., HMAC SHA256 or RSA). This header is Base64Url encoded.
Example Header:
{
"alg": "HS256",
"typ": "JWT"
} - Payload: The payload is a JSON object that contains the claims. Claims are statements about an entity (typically, the user) and additional data. Common claims include user ID, roles, permissions, and expiration time. The payload is also Base64Url encoded.
Example Payload:
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
} - Signature: The signature is created by taking the encoded header, the encoded payload, a secret (or a private key), and the algorithm specified in the header, and signing them. The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message was not changed along the way. The signature is NOT encoded.
Example Signature generation:
HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)
What Does JWT Decode Do?
Decoding a JWT involves several steps to extract and verify the information it contains. The primary purpose of decoding is to access the claims (data) within the payload and to ensure that the token has not been tampered with.
1. Splitting the Token:
The first step is to split the JWT string into its three component parts using the dot ('.') as a delimiter: header, payload, and signature.
2. Base64Url Decoding:
The header and payload parts, which are Base64Url encoded, are then decoded. This process converts the encoded strings back into their original JSON string representations.
- Header Decoding: The decoded header provides information about the token's type and the signing algorithm.
- Payload Decoding: The decoded payload reveals the claims, which represent the actual data being transmitted.
3. Signature Verification:
This is the most critical step for security. The signature is verified to ensure the token's integrity and authenticity. The process typically involves:
- Retrieving the signing algorithm from the decoded header.
- Obtaining the secret key (for symmetric algorithms like HS256) or the public key (for asymmetric algorithms like RS256) that corresponds to the algorithm used.
- Recreating the signature using the same algorithm, the original encoded header, the original encoded payload, and the secret/public key.
- Comparing the recreated signature with the signature provided in the JWT.
If the signatures match, it means the token was issued by the expected party and has not been altered since it was issued. If they do not match, the token is considered invalid or compromised.
Why Decode a JWT?
Decoding a JWT is essential for several reasons:
- Accessing User Information: To retrieve user-specific data like user ID, roles, or permissions from the payload for authorization checks.
- Checking Expiration: To determine if the token has expired and needs to be re-issued.
- Verifying Authenticity: To ensure that the token was legitimately issued by the server and hasn't been tampered with.
Many libraries and tools are available in various programming languages (e.g., `jwt.decode` in Python's `PyJWT`, `jwt.verify` in Node.js's `jsonwebtoken`) that abstract away the complexities of decoding and verification, making it easier for developers to work with JWTs securely.
More What Does in Daily Life
Also in Daily Life
More "What Does" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- JSON Web TokensCC-BY-4.0
- JSON Web Token - WikipediaCC-BY-SA-3.0
- RFC 7519 - JSON Web Token (JWT)BCP 78
Missing an answer?
Suggest a question and we'll generate an answer for it.