JWT Decoder for Swift
Free online jwt decoder with Swift code examples
Working with jwt decoder in Swift? Our free online jwt decoder helps Swift developers format, validate, and process data instantly. Below you will find Swift code examples using Swift-JWT / manual Base64 so you can achieve the same result programmatically in your own projects.
Try the JWT Decoder Online
Use our free JWT Decoder directly in your browser — no setup required.
Open JWT DecoderSwift Code Example
import Foundation
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
let parts = token.components(separatedBy: ".")
guard parts.count == 3 else { fatalError("Invalid JWT") }
func base64UrlDecode(_ str: String) -> Data? {
var base64 = str.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")
while base64.count % 4 != 0 { base64.append("=") }
return Data(base64Encoded: base64)
}
if let headerData = base64UrlDecode(parts[0]),
let payloadData = base64UrlDecode(parts[1]) {
let header = try JSONSerialization.jsonObject(with: headerData, options: [])
let payload = try JSONSerialization.jsonObject(with: payloadData, options: [])
print("Header:", header)
print("Payload:", payload)
}Quick Setup
Library: Swift-JWT / manual Base64
// SPM: https://github.com/vapor/jwt-kitSwift Tips & Best Practices
- JWT uses Base64URL encoding (different from standard Base64)
- Pad the Base64 string to a multiple of 4 before decoding
- Use vapor/jwt-kit for full JWT support in server-side Swift