Skip to content

Instantly share code, notes, and snippets.

@dbowling
Last active June 26, 2024 18:15
Show Gist options
  • Select an option

  • Save dbowling/29aadc805c873e86197e4fb4b235ab33 to your computer and use it in GitHub Desktop.

Select an option

Save dbowling/29aadc805c873e86197e4fb4b235ab33 to your computer and use it in GitHub Desktop.
Use jq to decode a JWT

Example usage

# Export the token for readability
export TOKEN="eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiZGJvd2xpbmciLCJleHAiOiIxNzE5NTA4OTI3IiwiZ3JvdXAiOiJkZXZlbG9wZXIifQ.sLMUZtxw-krA5hTAmKFcem-5uP7I35h6ypIfZKUEuXI"

# Use the command directly
jq -R 'split(".") | .[1] | @base64d | fromjson' <<< "$TOKEN"

# or the bash script
./jwt-decode.sh "$TOKEN"

# or the ZSH function
decode-jwt "$TOKEN"

Output:

{
  "user_id": "dbowling",
  "exp": "1719508927",
  "group": "developer"
}

Output should match the JWT.io debug.

#!/usr/bin/env bash
jq -R 'split(".") | .[1] | @base64d | fromjson' <<< "$1"
# Place this function in .zshrc or a location that is sourced from there.
function decode-jwt() {
jq -R 'split(".") | .[1] | @base64d | fromjson' <<< "$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment