Decode a JWT token
Read the JSON payload of a JSON Web Token — without verifying it.
function decodeJwtPayload(token) { const base64 = token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'); return JSON.parse(decodeURIComponent(Array.from(atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, '=')), char => `%${char.charCodeAt(0).toString(16).padStart(2, '0')}`).join(''))); }
Input & expected output
Input
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhbmFAZXhhbXBsZS5jb20iLCJuYW1lIjoiQW7DoSJ9.signature"Expected output
{
"sub": "[email protected]",
"name": "Aná"
}The edge case
Input
"x.eyJub3RlIjoiPz8-PjYiLCJ4Ijoifn5-Pz8_Pz8_In0.y"Expected output
{
"note": "??>>6",
"x": "~~~??????"
}Know the limits
- Decoding is not verification: anyone can forge a payload. Verify the signature with your JWT library before trusting any claim.
- The original replaced only the first - and _ of the base64url text, corrupting longer payloads; this version replaces all and restores padding.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Decode a JWT token from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Replaces every base64url - and _ (the original replaced only the first of each) and restores padding; documents that decoding is not verification. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Convert Celsius to Fahrenheit — Apply the temperature conversion without rounding the result.
- Convert hex to RGB — Expand #rgb if needed and read the red, green and blue channels.
- Convert URL parameters to object — Read a query string into an object; repeated keys become arrays.