How to escape Special Characters in JSON

hpandalai picture hpandalai · Nov 6, 2014 · Viewed 18k times · Source

We have a form which has a long paragraph for a scienctific application that contains characters like symbol beta(ß-arrestin) etc. We have a JSON service running on Mule that takes the data and persists to an oracle database. This particular element with long paragraph is giving 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 form element to which the scientists write we have no control. So on the Mule side how can we escape these characters automagically like java has URLEncoded. Many Thanks

Answer

kaulmonish picture kaulmonish · Apr 22, 2015

At the target you can receive the data as text/plain.

Clean it by running :

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

Convert it back to JSON data using any JSON library :

JSONObject inputParams = JSONObject.fromObject(input);

Hope it helps.