2

My REST API, which is build with Spring in Java, produces an invalid JSON object, because it contains multiple breaks in a string, which lead to the problem, that the string has an unexpected end and the rest doesn't count as part of the string anymore, example:

{
   "status": "Success",
   "message": "Lorem ipsum",
   "data": {
      "correct": [
         {
            "record": "ULTRA LONG
                 XML STRING
                       WITH BREAKS",
            "code": 0,
            "errors": []
         }
      ]
   }
}

The error arises in the data -> correct -> record string field, because it contains breaks which splits the original string.

My API endpoint serializes the above JSON like this:

@PostMapping(value="/check-records", 
    consumes=MediaType.APPLICATION_JSON_VALUE,
    produces=MediaType.APPLICATION_JSON_VALUE)
public Response checkRecords(@RequestBody(required=true) Records records) {
   // Check records
   return new Response("Success", "Lorem ipsum", data);
}

Response is a class, which automatically gets serialized into a JSON object after returning. data is a map in order to create the above JSON structure.

I couldn't find any suitable solution for my problem yet. Does anybody has an idea how I could remove all breaks, spaces or control characters before I serialize the JSON object?

I appreciate any kind of help, sheers! :)

Maik Hasler
  • 1,064
  • 6
  • 36
  • where did you get the xml string ? – hamwac5 Feb 11 '22 at 10:00
  • @hamwac5 I have a delphi application, which reads a xml file and makes a post request to the api after it – Maik Hasler Feb 11 '22 at 10:03
  • @matt I've saved the JSON to a file and removed all control characters by hand and the error is gone. – Maik Hasler Feb 11 '22 at 10:04
  • I suggest you to decode the xml string and decode it later – hamwac5 Feb 11 '22 at 10:06
  • record will be like : "record ": "PD94bWwgdmVyc2lvbj1cIjEug==", and you can decode it later in the front – hamwac5 Feb 11 '22 at 10:07
  • this can helps you : https://stackoverflow.com/questions/24496405/how-to-convert-text-file-of-base64-into-xml-fileor-any-other-type-of-file – hamwac5 Feb 11 '22 at 10:08
  • You should use a tool such as Postman to send a request to check whether the \n escape exists in the record field in the returned result. – HUTUTU Feb 11 '22 at 10:12
  • @HUTUTU I am able to remove all breaks before I do the request to my API. Nevertheless I says that there are some control characters left – Maik Hasler Feb 11 '22 at 10:14
  • How did you generate the JSON. You're serializing something and your generating invalid json. You're providing the string that gets serialized. Where are you generating that data? – matt Feb 11 '22 at 10:17
  • @matt Spring automatically serializes my given class into JSON. The steps before that are simply class string is string given by the request and so on. The real magic is done by Spring itself, because I said `produces=MediaType.APPLICATION_JSON_VALUE` – Maik Hasler Feb 11 '22 at 10:19
  • If you problem is on Delphi code part, and you cannot correct there, then if you change you java method to receive a simple raw request (String), you can then handle it, remove all control characters and transform to json (like using jackson for example). – pringi Feb 11 '22 at 10:20
  • @pringi Raw string okay, but could you give an example how to remove the special characters from the raw string then? – Maik Hasler Feb 11 '22 at 10:21
  • 1
    https://stackoverflow.com/questions/14028716/how-to-remove-control-characters-from-java-string – pringi Feb 11 '22 at 10:22
  • @Abra That's the thing, I don't see any control characters - The string looks like a normal string to me – Maik Hasler Feb 11 '22 at 10:22
  • 1- If you have control over the generated JSON, then format it first..before sending it towards a REST API. https://stackoverflow.com/questions/8596161/json-string-tidy-formatter-for-java To validate if your JSON is proper, please use https://jsonformatter.curiousconcept.com/# – JCompetence Feb 11 '22 at 10:29

1 Answers1

1

Thanks to @pringi. He suggested to use a regex to remove all control characters in Java before I serialize the JSON object.

String record = orginalRecord.replaceAll("[\\\\p{Cntrl}^\\r\\n\\t]+", "")

You can find more informations about regex in the original question: How to remove control characters from java string?

Maik Hasler
  • 1,064
  • 6
  • 36