Avoiding special characters in JSON

We have a form that has a long paragraph for a scientific application containing symbols such as beta symbol (ß-preservein), etc. We have a JSON service running on Mule that takes data and stores it in the oracle database. This particular item with a long paragraph gives me an error in RAML / JSON. Below is the error

com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 9)): has to be escaped using backslash to be included in string value 

The element of form that scientists write, we do not control. So, on the Mule side, how can we avoid these characters automatically, for example, java has URLEncoded. Many thanks

+8
java json mule raml
source share
2 answers

In your case, it looks like the input is incorrect. It must be in the encoding supported by the JSON specification : UTF-8 (default), UTF-16, or UTF-32. Therefore, I am not sure that the following applies. Nevertheless...

For most applications, I would recommend JSON to Object mapping , which will take care of shielding. Otherwise, you can directly call Jackson (the JSON library used by Mule) to delete strings .

Here is an example that you can use in MEL. String.valueOf necessary because quoteAsString returns char[] :

 <configuration> <expression-language> <import class="org.codehaus.jackson.io.JsonStringEncoder" /> <global-functions> def quoteJSONString(s) { String.valueOf(JsonStringEncoder.getInstance().quoteAsString(s)) } </global-functions> </expression-language> </configuration> 
+1
source share

In the target you can receive data as text / simple.

Clean it by running:

 input.replaceAll("\\p{Cc}", ""). 

Convert it back to JSON data using any JSON library:

 JSONObject inputParams = JSONObject.fromObject(input); 

Hope this helps.

+2
source share

All Articles