Skip to content

Instantly share code, notes, and snippets.

Last active December 6, 2023 05:01
  • Star 99 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Example for decoding a JWT Payload with your Shell (bash, zsh...)


Add this to your .profile, .bashrc, .zshrc...

decode_base64_url() {
  local len=$((${#1} % 4))
  local result="$1"
  if [ $len -eq 2 ]; then result="$1"'=='
  elif [ $len -eq 3 ]; then result="$1"'=' 
  echo "$result" | tr '_-' '/+' | openssl enc -d -base64

   decode_base64_url $(echo -n $2 | cut -d "." -f $1) | jq .

# Decode JWT header
alias jwth="decode_jwt 1"

# Decode JWT Payload
alias jwtp="decode_jwt 2"


jwtp eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ


  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
Copy link

Using Jq

function jwt_decode(){
    jq -R 'split(".") | .[1] | @base64d | fromjson' <<< "$1"


╰─ jwt_decode eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022

Copy link

@vithalreddy Perfect cheers :)

Copy link

@vithalreddy beautiful, thanks !

Copy link

Solution from @vithalreddy works perfectly across platforms, thanks!

Copy link

Perfect. Thanks @vithalreddy.

Copy link

Awesome when people share their knowledge for free. Thanks men!

I will add your script to my jarvis assistant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment