Fixed: JJWT Keeps an Empty Custom Claim Although Its Javadoc Says It Removes It
JJWT's documentation for JwtBuilder.claim says that a null or empty value removes the claim. JJWT 0.13.0 kept an empty custom claim, though: a nickname of "" went into the token as "nickname":"". Only null removes a custom claim, so the code has to leave an empty one out itself.
What I Was Doing
I was checking which values JJWT writes into a custom claim, for posts about custom claims in Java. JJWT's documentation for claim says that an empty value removes the claim, so I expected an empty string to leave the claim out of the token.
The example below shows it on its own: Ada hasn't set a nickname, so her nickname is an empty string.
What I Expected
A token without a nickname claim. The documentation of JwtBuilder.claim says: "Sets a JWT claim, overwriting any existing claim with the same name. A null or empty value will remove the claim entirely."
What Happened Instead
Create a folder named demo, with this pom.xml in it:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jjwt.version>0.13.0</jjwt.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- jjwt-jackson asks for Jackson 2.12.7.1. This makes Maven use the newest Jackson 2 instead. -->
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.23.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
It lists JJWT's three parts: jjwt-api, which the code calls, and jjwt-impl and jjwt-jackson, which do the work while the program runs. The jackson-bom entry makes Maven use Jackson 2.23.0 instead of the older version jjwt-jackson asks for, and the exec-maven-plugin entry names com.example.Main as the class to run.
Then the program, in src/main/java/com/example/Main.java:
package com.example;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import java.nio.charset.StandardCharsets;
import javax.crypto.SecretKey;
public class Main {
public static void main(String[] args) {
// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode("C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64="));
String nickname = ""; // Ada hasn't set a nickname
String token = Jwts.builder()
.subject("ada")
.claim("nickname", nickname)
.signWith(key)
.compact();
// The payload is the token's middle piece, base64url-encoded JSON.
String payload = token.split("\\.")[1];
System.out.println(new String(Decoders.BASE64URL.decode(payload), StandardCharsets.UTF_8));
}
}
It signs a token with sub and the empty nickname, then prints the token's payload: the middle piece of the token, decoded from base64url.
In the demo folder, build and run it:
mvn -q compile exec:java
{"sub":"ada","nickname":""}

-q keeps Maven quiet, compile builds the program and exec:java runs it. The command is the same in zsh, bash and PowerShell. The nickname claim is in the token, with an empty string as its value.
What I Checked
- Other empty values. A string of two spaces, an empty list and an empty map were kept too:
{"sub":"ada","nickname":" "},{"sub":"ada","roles":[]}and{"sub":"ada","quota":{}}. So were empty values added withclaims().addandclaims(Map). Onlynullremoved a custom claim:{"sub":"ada"}. - Registered claims. An empty value did remove a registered claim.
.claim("iss", "")and.issuer("")both leftissout, and.claim("sub", "")leftsubout. - The release notes. JJWT 0.12.3 changed this on purpose: "Empty custom claims values are (re-)enabled which was the behavior in <= 0.11.5." The change came from jwtk/jjwt#858, "Support empty claims values".
- The code. In JJWT 0.13.0,
ParameterMap.putsends a registered name to a method that removes an empty value, and a custom name to one that removes onlynull. - The documentation on JJWT's main branch. It still has the sentence about empty values.
So JJWT keeps an empty custom claim on purpose, and the documentation of claim describes the behaviour from before 0.12.3. I reported the mismatch as jwtk/jjwt#1088.
The Fix
Add the claim only when it has a value. Keep the builder in a variable, add nickname inside an if, and sign afterwards:

package com.example;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import java.nio.charset.StandardCharsets;
import javax.crypto.SecretKey;
public class Main {
public static void main(String[] args) {
// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode("C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64="));
String nickname = ""; // Ada hasn't set a nickname
JwtBuilder builder = Jwts.builder().subject("ada");
// JJWT keeps an empty custom claim, so add the claim only when there's a value.
if (!nickname.isEmpty()) {
builder.claim("nickname", nickname);
}
String token = builder.signWith(key).compact();
// The payload is the token's middle piece, base64url-encoded JSON.
String payload = token.split("\\.")[1];
System.out.println(new String(Decoders.BASE64URL.decode(payload), StandardCharsets.UTF_8));
}
}
JwtBuilder builder = Jwts.builder().subject("ada");keeps the builder in a variable, so the code can decide what to add before it signs.JwtBuilderis imported fromio.jsonwebtoken.if (!nickname.isEmpty())addsnicknameonly when it has text.builder.signWith(key).compact()signs the token with whatever was added.
The Result
Run it again:
mvn -q compile exec:java
{"sub":"ada"}

The token has no nickname claim.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing "" for a custom claim that has no value, trusting the documentation to remove it | The token carries "nickname":"" | Add the claim only when it has a value |
| Passing an empty list or map to mean "none" | The token carries [] or {} | Leave the claim out in the same way |
| Expecting custom and registered claims to behave alike | An empty iss or sub is removed, an empty custom claim is kept | Decide in your own code which claims go in |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A custom claim with "", [] or {} in a token made with JJWT | The value passed to claim was empty | Add the claim only when it has a value, or pass null |
iss or sub is missing from the token | Its value was an empty string, and an empty value removes a registered claim | Check the value before you pass it |
FAQ
Is this a bug in JJWT?
Keeping the claim is what JJWT means to do since 0.12.3, as its release notes say. The documentation of claim is what's out of date, and jwtk/jjwt#1088 reports it.
Can I pass null instead?
Yes. A null value removes a custom claim, and JJWT's documentation says so too. The if in the fix makes the decision visible where the claim is added.
Do claims().add and claims(Map) behave the same?
Yes. An empty value under a custom name was kept through both of them.
Conclusion
JJWT's documentation says an empty value removes a claim, but since 0.12.3 that's true only for registered claims such as iss. An empty custom claim stays in the token, so leave it out in your own code: add the claim only when it has a value.
Additional Resources
- JwtBuilder, in the JJWT 0.13.0 API documentation.
- JJWT 0.12.3 release notes.
- jwtk/jjwt#858: Support empty claims values.
- jwtk/jjwt#1088, the report about the documentation.
- Custom Claims, in the JJWT README.
