How to decode Json Web Token in Javascript?

Forums JavaScriptHow to decode Json Web Token in Javascript?
Staff asked 1 month ago

Answers (2)

Add Answer
Umang Ramani Marked As Accepted
Staff answered 1 month ago

This code can be used to decode the web token.

var decodedToken = JSON.parse(atob(token.split('.')[1]));

 

Staff answered 1 month ago
function parseJwt (token) {
    var base64Url = token.split('.')[1];
    var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));

    return JSON.parse(jsonPayload);
};

 

Subscribe

Select Categories