Search

Dark theme | Light theme

October 20, 2026

Nushell Niceties: Parsing JWT

The following blog post by Karl Tryggvason shows a simple script to get data from a JSON Web Token (JWT) using jq. It is basically splitting the token on the . and then convert each part from base64 encoding. As extra functionality the timestamp fields exp and iat are converted to human readable dates. Let's see how you can use Nushell to get the same results.

The following custom Nushell command implements the same functionality. The command uses some helper internal commands to convert the timestamp values and decoding from base64. The token is split using split row and then each part is transformed to a record with keys and values.

# Extract header and payload from JSON Web Token (JWT).
#
# Use as command:
#   jwt 'eyJhbGciOiJIU...'
#
# Use in pipeline:
#   'eyJhbGciOiJIU...' | jwt
#
def jwt [
  token?: string # JWT token string (optional, can be passed via pipeline)
] : [string -> record, nothing -> record] {

  # Convert timestamp in milliseconds to date value.
  let to_date = { into datetime -f '%s' | format date '%+' }

  # Split token on . and get first 2 rows containing the header and payload.
  # Decode each to record.
  let parts = ($in | default $token)
    | split row '.'
    | take 2
    | each { decode base64 --nopad --url | decode utf-8 | from json }

  {
    header: $parts.0
    payload: ($parts.1
      | update exp? $to_date
      | update iat? $to_date
      | update nbf? $to_date)
  }
}

You can use the jwt command both as a command with an input argument and as pipeline element. The output is a record with header and payload data from the JWT.

> let $jwt_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
['
> jwt $jwt_token
 header     alg   HS256
            typ   JWT
 payload    sub    1234567890
            name   John Doe
            iat    2018-01-18T01:30:22+00:00
> $jwt_token | jwt
 header     alg   HS256
            typ   JWT
 payload    sub    1234567890
            name   John Doe
            iat    2018-01-18T01:30:22+00:00

Written with Nushell 0.116.1.