How to Decode a JWT Without Verifying It in Ruby with JWT.decode
The jwt gem's JWT.decode normally verifies a token. Pass nil as the key and false as the third argument, and it only reads the token, returning its payload and header without checking anything. That's right for reading a token and wrong for trusting one.
Syntax
require 'jwt'
<payload>, <header> = JWT.decode(<token>, nil, false)
| Part | What it does |
|---|---|
<token> | The JWT as a string. |
nil | The key. None is needed when nothing is verified. |
false | The verify argument: turns the signature and claim checks off. |
<payload>, <header> | The two hashes JWT.decode returns, unpacked into two variables. |
The jwt gem documents JWT.decode as "Decodes a JWT to extract the payload and header", with the third parameter described as "verify [Boolean] whether to verify the JWT signature." Its decoder checks the algorithm, the key, the signature and the claims only if @verify.
The Example
The example reads a token that another service signed with a key this program doesn't have. The token expired on 1 December 2025, and its header names the key that signed it, key-2025-12, in a kid.
Prerequisites
- Ruby, which comes with Bundler. This guide uses Ruby 4.0.10.
Step 1: Create the Project
mkdir demo
cd demo
bundle init
bundle add jwt
bundle init creates a Gemfile, and bundle add jwt adds the jwt gem, version 3.3.2 here, and installs it.
Step 2: Write the Code
Create main.rb in the demo folder:

require 'jwt'
# A token from another service, signed with a key this program doesn't have.
# It expired on 1 December 2025.
token = 'eyJhbGciOiJIUzI1NiIsImtpZCI6ImtleS0yMDI1LTEyIiwidHlwIjoiSldUIn0.eyJzdWIiOiJhZGEiLCJyb2xlIjoicmVhZGVyIiwiaWF0IjoxNzY0NTQ3MjAwLCJleHAiOjE3NjQ1NTA4MDB9.2EjUXD-_uc78GAdKpsWk7GOlMQztlsdWKQkT-RdKFKE'
payload, header = JWT.decode(token, nil, false)
p header
p payload
Step 3: Run It
bundle exec ruby main.rb
{"alg" => "HS256", "kid" => "key-2025-12", "typ" => "JWT"}
{"sub" => "ada", "role" => "reader", "iat" => 1764547200, "exp" => 1764550800}

JWT.decode returned the header and every claim without the key, and didn't mind that exp, 1764550800, is 1 December 2025 at 01:00 UTC.
Decoding Is Not Verifying
Anyone can write a token with any claims, so these claims prove nothing. Use them to read, never to decide who someone is or what they may do. To trust a token, verify it with the key and the algorithm you expect:
payload, _header = JWT.decode(token, key, true, algorithm: 'HS256')
That call checks the signature and the expiry, and raises a JWT::DecodeError when either fails.
Reading the header first is the one common server-side use: a service with several keys reads kid and picks that key from its own list. The header is just as forgeable as the claims, so look it up only among keys you already trust.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Trusting the payload from JWT.decode(token, nil, false) | Anyone can send a token with any claims | Decode with the key, true and the algorithm |
| Expecting the expiry to be checked | With false, no claim is checked: an expired token decoded without an error | Verify; that checks exp |
| Forgetting the order of the return values | JWT.decode returns the payload first, then the header | Unpack as payload, header = ... |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
JWT::MalformedTokenError: Not enough or too many segments | The string isn't three pieces separated by dots | Check what you passed, including a Bearer prefix |
cannot load such file -- jwt (LoadError) | The jwt gem isn't installed for the Ruby running the script | Run bundle add jwt in the project, then bundle exec ruby main.rb |
FAQ
Is there another way to read a token without verifying it?
Yes. The gem's newer JWT::EncodedToken class has unverified_payload, which "Returns the payload of the JWT token without requiring the signature to have been verified." Its plain payload refuses until you verify.
Is it safe to decode tokens this way in a client?
For reading, yes, for example to show the user's name or to refresh before exp. The server that receives the token still verifies it.
Conclusion
JWT.decode(token, nil, false) returns a token's payload and header without a key and without any checks. Use it to look inside a token; decode with the key, true and the algorithm before you trust it.
Additional Resources
- ruby-jwt, the jwt gem, with its README, and its lib/jwt.rb.
- RFC 8725, section 3.10: Do Not Trust Received Claims.
