158

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?

6
  • 1
    Are you creating the JSON yourself? Basically, it sounds like you've got broken data... so either fix it if you can, or complain to whoever's producing it. Commented Jul 21, 2015 at 10:59
  • 1
    As discussed in this StackOverflow answer, does your JSON validate correctly via jsonlint.com? Commented Jul 21, 2015 at 14:53
  • Intellij idea validates it as soon as you open the .json file. Try it! Commented Mar 6, 2019 at 15:55
  • I had code 9 issue - it was because of [TAB] character Commented Dec 2, 2020 at 15:10
  • 1
    @Alexander.Iljushkin, i was going to say the same thing, this was the mistake i did :) Commented Jul 21, 2024 at 15:13

4 Answers 4

140

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"}
Sign up to request clarification or add additional context in comments.

3 Comments

unfortunately, the reason why I have new lines is because the string is big and I would like it to be readable. Actual new lines make the string helps toward this goal
@juanchito Adding newlines to literals change what the literals are. You cannot add them there solely for readability, because it changes their meaning. Put the newlines between tokens instead of inside them. And regardless, format things for the server when you are submitting to it; human readability before or after is not relevant and should be handled separately.
You can trim newlines before sending the payload, getting both readability and correct JSON.
94

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.

6 Comments

how to implement this in xml ? I have <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="serializationInclusion" value="NON_NULL"/> </bean> </property> <property name="supportedMediaTypes" value="application/json"/> </bean> </mvc:message-converters> </mvc:annotation-driven> . I am using Jackson 2.7. and Spring4.3.2
Why don't you try this: stackoverflow.com/questions/5349362/… Or overriding that bean: MappingJackso‌​n2HttpMessageConvert‌​er
@hoang where to delare this in java as I am trying to capture this illegal data, I have asked the question which is here stackoverflow.com/questions/49676720/…, can you please help me out.
what is mapper. please, provide context on how it is used
this is ObjectMapper of Jackson, please see: github.com/FasterXML/jackson
|
1

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

1 Comment

For me - on Salesforce platform - it was "\" to "\\" that was actually needed. At least in my case it occured because of new line characters in my JSON. \n had to be changed to \\n. This is due to the fact that JSON requires control characters to be escaped. See this excellent comment here.
-1

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

1 Comment

newline is \n not /n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.