title: How to Get JSON String for header/payload from JWT token using java-jwt
body: <p>Is there a method in java-jwt that will return the token as a single JSON string (or at least the header and payload as two JSON strings)? Does JJWT support this?</p>
  
  <p>Looking at the tutorial at <a href="https://github.com/auth0/java-jwt" rel="nofollow noreferrer">https://github.com/auth0/java-jwt</a> I'm not seeing this method (there are individual getters for individual pieces of the message but I'm not seeing anything that returns the complete message as a JSON string).  </p>
  
  <p>The getHeader() and getHeader() methods return the encoded strings.  I'm trying to get the headers and payload as JSON (similar to what is shown at <a href="https://jwt.io/" rel="nofollow noreferrer">https://jwt.io/</a>).  How do I do this?</p>
  
  <p>Here's the code so far:</p>
  
  <pre><code>import org.junit.Test;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import com.auth0.jwt.JWT;
  import com.auth0.jwt.JWTCreator;
  import com.auth0.jwt.JWTVerifier;
  import com.auth0.jwt.algorithms.Algorithm;
  import com.auth0.jwt.interfaces.DecodedJWT;
  
  public class CreateTokenExampleIntegrationTest {
  
      private static final Logger logger = LoggerFactory.getLogger(CreateTokenExampleIntegrationTest.class);
  
      @Test
      public void shouldCreateToken() {
          logger.info("Starting test...");
          // create the token
          String key = "foobarkey";
          Algorithm alg = Algorithm.HMAC256(key);
          String issuer = "me";
          JWTCreator.Builder jwt = JWT.create();
          jwt.withIssuer(issuer);
          jwt.withClaim("createdBy", "CreateTokenExampleIntegrationTest");
          jwt.withClaim("purpose", "test");
          jwt.withClaim("msg", "Hello World.");
          String token = jwt.sign(alg);
          logger.info("Created token: \n" + token);
          // read the token
          JWTVerifier verifier = JWT.require(alg)
                  .withIssuer(issuer)
                  .build();
          DecodedJWT decoded = verifier.verify(token);
          logger.info("Header: \n" + decoded.getHeader());
          logger.info("Payload: \n" + decoded.getPayload());
          logger.info("Done.");
      }
  
  }
  </code></pre>
  
  <p>And here's the output:</p>
  
  <pre><code>2020-05-19 08:23:31,387 08:23:31.387 [main] INFO  (CreateTokenExampleIntegrationTest.java:19) - Starting test...
  2020-05-19 08:23:32,226 08:23:32.226 [main] INFO  (CreateTokenExampleIntegrationTest.java:30) - Created token: 
  eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtc2ciOiJIZWxsbyBXb3JsZC4iLCJjcmVhdGVkQnkiOiJDcmVhdGVUb2tlbkV4YW1wbGVJbnRlZ3JhdGlvblRlc3QiLCJwdXJwb3NlIjoidGVzdCIsImlzcyI6Im1lIn0.VmYsToj1PKBzJKQuXEyiKuJA-GkNVit0Ylh44dVF2UI
  2020-05-19 08:23:32,273 08:23:32.273 [main] INFO  (CreateTokenExampleIntegrationTest.java:36) - Header: 
  eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
  2020-05-19 08:23:32,273 08:23:32.273 [main] INFO  (CreateTokenExampleIntegrationTest.java:37) - Payload: 
  eyJtc2ciOiJIZWxsbyBXb3JsZC4iLCJjcmVhdGVkQnkiOiJDcmVhdGVUb2tlbkV4YW1wbGVJbnRlZ3JhdGlvblRlc3QiLCJwdXJwb3NlIjoidGVzdCIsImlzcyI6Im1lIn0
  2020-05-19 08:23:32,273 08:23:32.273 [main] INFO  (CreateTokenExampleIntegrationTest.java:38) - Done.
  </code></pre>
  
  <h2>EDIT: This is a complete solution based on the accepted answer:</h2>
  
  <h3>Code:</h3>
  
  <pre><code>import org.apache.commons.codec.binary.Base64;
  import org.apache.commons.codec.binary.StringUtils;
  import org.junit.Test;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import com.auth0.jwt.JWT;
  import com.auth0.jwt.JWTCreator;
  import com.auth0.jwt.JWTVerifier;
  import com.auth0.jwt.algorithms.Algorithm;
  import com.auth0.jwt.interfaces.DecodedJWT;
  
  public class CreateTokenExampleIntegrationTest {
  
      private static final Logger logger = LoggerFactory.getLogger(CreateTokenExampleIntegrationTest.class);
  
      @Test
      public void shouldCreateToken() {
          logger.info("Starting test...");
          // create the token
          String key = "foobarkey";
          Algorithm alg = Algorithm.HMAC256(key);
          String issuer = "me";
          JWTCreator.Builder jwt = JWT.create();
          jwt.withIssuer(issuer);
          jwt.withClaim("createdBy", "CreateTokenExampleIntegrationTest");
          jwt.withClaim("purpose", "test");
          jwt.withClaim("msg", "Hello World.");
          String token = jwt.sign(alg);
          logger.info("Created token: \n" + token);
          // read the token
          JWTVerifier verifier = JWT.require(alg)
                  .withIssuer(issuer)
                  .build();
          DecodedJWT decoded = verifier.verify(token);
          // header
          String encHeader = decoded.getHeader();
          String header = decode(encHeader);
          logger.info("Header: \n" + encHeader + "\n" + header);
          // payload
          String encPayload = decoded.getPayload();
          String payload = decode(encPayload);
          logger.info("Payload: \n" + encPayload + "\n" + payload);
          // done
          logger.info("Done.");
      }
  
      public String decode(final String base64) {
          return StringUtils.newStringUtf8(Base64.decodeBase64(base64));
      }
  
  }
  </code></pre>
  
  <h3>Output:</h3>
  
  <pre><code>2020-05-19 10:13:29,146 10:13:29.146 [main] INFO  (CreateTokenExampleIntegrationTest.java:21) - Starting test...
  2020-05-19 10:13:30,031 10:13:30.031 [main] INFO  (CreateTokenExampleIntegrationTest.java:32) - Created token: 
  eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtc2ciOiJIZWxsbyBXb3JsZC4iLCJjcmVhdGVkQnkiOiJDcmVhdGVUb2tlbkV4YW1wbGVJbnRlZ3JhdGlvblRlc3QiLCJwdXJwb3NlIjoidGVzdCIsImlzcyI6Im1lIn0.VmYsToj1PKBzJKQuXEyiKuJA-GkNVit0Ylh44dVF2UI
  2020-05-19 10:13:30,102 10:13:30.102 [main] INFO  (CreateTokenExampleIntegrationTest.java:41) - Header: 
  eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
  {"typ":"JWT","alg":"HS256"}
  2020-05-19 10:13:30,103 10:13:30.103 [main] INFO  (CreateTokenExampleIntegrationTest.java:45) - Payload: 
  eyJtc2ciOiJIZWxsbyBXb3JsZC4iLCJjcmVhdGVkQnkiOiJDcmVhdGVUb2tlbkV4YW1wbGVJbnRlZ3JhdGlvblRlc3QiLCJwdXJwb3NlIjoidGVzdCIsImlzcyI6Im1lIn0
  {"msg":"Hello World.","createdBy":"CreateTokenExampleIntegrationTest","purpose":"test","iss":"me"}
  2020-05-19 10:13:30,103 10:13:30.103 [main] INFO  (CreateTokenExampleIntegrationTest.java:47) - Done.
  </code></pre>