JWT Parser
Parse JSON Web Tokens (JWT) into their three components: header, payload, and signature. Understand the structure and contents of any JWT.
JWT Structure
A JWT consists of three Base64URL-encoded parts separated by dots:
``
xxxxx.yyyyy.zzzzz
header.payload.signature
`
| Component | Contents | Encoding |
HeaderAlgorithm (alg) and token type (typ)Base64URL
PayloadClaims (iss, sub, exp, iat, etc.)Base64URL
| Signature | Cryptographic signature | Base64URL |
JWT Parser Implementation
`javascript
function parseJWT(token) {
const parts = token.split('.');
if (parts.length !== 3) {
throw new Error('Invalid JWT format - must have 3 parts');
}
const decode = (str) => {
// Base64URL decode
const base64 = str.replace(/-/g, '+').replace(/_/g, '/');
return JSON.parse(atob(base64));
};
return {
header: decode(parts[0]),
payload: decode(parts[1]),
signature: parts[2]
};
}
// Example usage
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
const parsed = parseJWT(token);
// header: { alg: 'HS256', typ: 'JWT' }
// payload: { sub: '1234567890', name: 'John Doe', iat: 1516239022 }
``
Common JWT Claims
| Claim | Full Name | Description | issIssuerWho created the token subSubjectUser/entity identifier audAudienceIntended recipients expExpirationWhen token expires (Unix timestamp) iatIssued AtWhen token was created nbfNot BeforeToken not valid before this time jtiJWT IDUnique token identifier