Extract claims from jwt token in go


Should you need to extract claims from a JWT token issued by an identity provider, the following code can be used for that purpose.

package main

import (

func main() {
	tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" // typically obtained from and 'Authorization' http header
	token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{})
	if err != nil {
		fmt.Printf("Error %s", err)
	if claims, ok := token.Claims.(jwt.MapClaims); ok {
		// obtains claims
		sub := fmt.Sprint(claims["sub"])
		name := fmt.Sprint(claims["name"])
		// and so on and on
		// ...
		fmt.Printf("sub = %s\r\n", sub)
		fmt.Printf("name = %s", name)


Hope it helps!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.