brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: jq @base64d reads the payload of a JWT in the terminal

Jq
~/tips/tip-jq-base64d-jwt-payload

echo "$TOKEN" | jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'

Decode the middle part of a JWT to see its claims and expiry time.

When an API token stops working, I want to see what’s inside it, especially the expiry time. A JWT is three base64 parts separated by dots, and jq can decode the middle one.

echo "$TOKEN" | jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'
echo "$TOKEN" | jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | .exp | todate'
# "2026-01-01T00:00:00Z"

-R reads the token as a plain string, split(".")[1] takes the payload part, @base64d decodes it and fromjson parses the result. The second line goes one step further and turns the exp claim, a Unix timestamp, into a readable date with todate.

The two gsub calls are there because JWTs use the URL safe base64 alphabet, with - and _ instead of + and /. jq’s decoder expects the standard alphabet and errors out with “is not valid base64 data” on a token that happens to contain one of those characters. The missing = padding at the end is no problem.

This only reads the token. It doesn’t check the signature, so it tells you nothing about whether the token is genuine. And a token is a password while it’s valid, so I’d paste it into a terminal and never into a random website that offers to decode it.

Further reading: jq manual: @base64d, RFC 7519 (JWT)

More Jq tips jq -r '.files[] | @sh' data.json jq -n -c 'limit(3; range(1; 1000000000))' jq -n -c '$ARGS.positional' --args a "b c" jq 'with_entries(.key |= ascii_upcase)' jq '.replicas |= . + 1' deploy.json All Jq tips (20) All topics

Link