How can I get the name and email from a token .
Structure of the token using jwt.io http://prntscr.com/yzyf2b
Any help is appreciated.
Update full solution with the help of below posts so credits to them .
            String jwtToken = token;
            System.out.println("------------ Decode JWT ------------");
            String[] split_string = jwtToken.split("\\.");
            String base64EncodedHeader = split_string[0];
            String base64EncodedBody = split_string[1];
            String base64EncodedSignature = split_string[2];
            System.out.println("~~~~~~~~~ JWT Header ~~~~~~~");
            Base64 base64Url = new Base64(true);
            String header = new String(base64Url.decode(base64EncodedHeader));
            System.out.println("JWT Header : " + header);
            System.out.println("~~~~~~~~~ JWT Body ~~~~~~~");
            String body = new String(base64Url.decode(base64EncodedBody));
            System.out.println("JWT Body : " + body);
            JSONObject jsonObject = new JSONObject(body);
            System.out.println(jsonObject.get("email"));
            System.out.println(jsonObject.get("name"));
        
                A JWToken has the following structure Header.Body.Signature. Hence, first you should split the token into three parts, namely Header, Body and Signature.
For that you can use
String[] token_part = jwtToken.split("\\.");
then apply what you already have done but for the token_part[1] (i.e., the payload or Body), namely:
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
String a = new String(decoder.decodeBuffer(token_part[1]));
For the decoding part, alternatively, you can try this SO thread.
After that you can use the org.json.JSONObject to parse the JSON. An example:
public class Token {
    public static void main(String[] args) {
        String token = "{\"name\":\"john doe\", \"email\": \"[email protected]\"}";
        JSONObject jsonObject = new JSONObject(token);
        System.out.println(jsonObject.get("email"));
        System.out.println(jsonObject.get("name"));
    }
}
Output:
[email protected]
john doe
Full Example:
    String jwtToken = token;
    System.out.println("------------ Decode JWT ------------");
    String[] split_string = jwtToken.split("\\.");
    String base64EncodedHeader = split_string[0];
    String base64EncodedBody = split_string[1];
    String base64EncodedSignature = split_string[2];
    System.out.println("~~~~~~~~~ JWT Header ~~~~~~~");
    Base64 base64Url = new Base64(true);
    String header = new String(base64Url.decode(base64EncodedHeader));
    System.out.println("JWT Header : " + header);
    System.out.println("~~~~~~~~~ JWT Body ~~~~~~~");
    String body = new String(base64Url.decode(base64EncodedBody));
    System.out.println("JWT Body : " + body);
    JSONObject jsonObject = new JSONObject(body);
    System.out.println(jsonObject.get("email"));
    System.out.println(jsonObject.get("name"));
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With