Search code examples
javajsonjacksonjwtjjwt

Decripting by JJWT a JSON String removed quotation marks -Java


My web site send a JWT (built in php) to a my app developped in Java.

The JWT contains a JSON String inside a custom field called DATI. I use the library JJWT in order to decript the string contained inside DATI field:

Claims MY_CLAIMS = Jwts.parser().setSigningKey(SECRET_KEY__Byte).parseClaimsJws(STRING_JWT).getBody(); 
ArrayList ARRAY = MY_CLAIMS .get("DATI", ArrayList.class);
String DECODED_STRING_INSIDE_DATI =String.valueOf(ARRAY);

I get the string "DECODED_STRING_INSIDE_DATI" (that is a JSON String) in the correct way, but for some reason the quotation marks (") are removed:

[{id=3, id_rivenditore=-1, id_cliente=-1, ip_address=192.168.1.6, nome=DonalDuck, note=ByBye, enabled=1}]

I tested STRING_JWT in "https://jwt.io/", and there I get correctly the quotation marks:

{
  "iss": "www.mySite.it",
  "exp": 1536913435,
  "sub": "WebApp",
  "DATI": [
    {
      "id": "3",
      "id_rivenditore": "-1",
      "id_cliente": "-1",
      "ip_address": "192.168.1.6",
      "nome": "DonalDuck",
      "note": "ByBye",
      "enabled": "1"
    }
  ]
}

I really don't know how to solve it, because I cannot read the JSON string in the right way. I use jackson library to read Json String


Solution

  • This might help,

    You already have ArrayList containing required claims as,

    ArrayList ARRAY = MY_CLAIMS.get("DATI", ArrayList.class);
    

    To get a JSON string of claims included in this ArrayList, try below code.

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(out, ARRAY);
    byte[] data = out.toByteArray();
    String str = new String(data);
    

    str contains properly formatted JSON string (with quotation marks).