JsonParseException : Illegal unquoted character ((CTRL-CHAR, code 10)

JavaJsonCharacter EncodingApache Httpclient-4.x

Java Problem Overview


I'm trying to use org.apache.httpcomponents to consume a Rest API, which will post JSON format data to API.

I get this exception:

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

The reason is because ctrl-char is included in the JSON string.

Is there any way to replace this or some other solution?

Java Solutions


Solution 1 - Java

This can happen if you have a newline (or other control character) in a JSON string literal.

{"foo": "bar
baz"}

If you are the one producing the data, replace actual newlines with escaped ones "\\n" when creating your string literals.

{"foo": "bar\nbaz"}

Solution 2 - Java

Using

mapper.configure(
    JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), 
    true
);

See javadoc:

/**
 * Feature that determines whether parser will allow
 * JSON Strings to contain unescaped control characters
 * (ASCII characters with value less than 32, including
 * tab and line feed characters) or not.
 * If feature is set false, an exception is thrown if such a
 * character is encountered.
 *<p>
 * Since JSON specification requires quoting for all control characters,
 * this is a non-standard feature, and as such disabled by default.
 */

Old option JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS was deprecated since 2.10.

Please see also github thread.

Solution 3 - Java

On Salesforce platform this error is caused by /, the solution is to escape these as //.

Solution 4 - Java

This error occurs when you are sending JSON data to server. Maybe in your string you are trying to add new line character by using /n.

If you add / before /n, it should work, you need to escape new line character.

"Hello there //n start coding"

The result should be as following

Hello there
start coding

Solution 5 - Java

JsonParseException : Illegal unquoted character ((CTRL-CHAR, code 10)

Solution: Exists in your text a chr TAB, before put text in the json, use replace chr TAB to \t

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjian zhongView Question on Stackoverflow
Solution 1 - JavapyrospadeView Answer on Stackoverflow
Solution 2 - JavahoangView Answer on Stackoverflow
Solution 3 - JavaDaniel SokolowskiView Answer on Stackoverflow
Solution 4 - JavaSadaqat AliView Answer on Stackoverflow
Solution 5 - JavaAlexsandro CostaView Answer on Stackoverflow