147

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?

javaPlease42
  • 4,699
  • 7
  • 36
  • 65
jian zhong
  • 1,471
  • 2
  • 9
  • 4
  • 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. – Jon Skeet Jul 21 '15 at 10:59
  • 1
    As discussed in [this StackOverflow answer](http://stackoverflow.com/a/17487154/1720082), does your JSON validate correctly via [jsonlint.com](http://jsonlint.com/)? – Eric McCormick Jul 21 '15 at 14:53
  • Intellij idea validates it as soon as you open the .json file. Try it! – Gaurav Mar 06 '19 at 15:55
  • I had code 9 issue - it was because of [TAB] character – yossico Dec 02 '20 at 15:10

5 Answers5

131

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"}
pyrospade
  • 7,870
  • 4
  • 36
  • 52
  • 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 Apr 30 '21 at 16:52
  • 2
    @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. – Matthew Read Oct 17 '21 at 17:02
  • 1
    You can trim newlines before sending the payload, getting both readability and correct JSON. – Jade Steffen Feb 03 '22 at 21:04
84

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.

Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
hoang
  • 1,444
  • 11
  • 19
  • how to implement this in xml ? I have ` ` . I am using Jackson 2.7. and Spring4.3.2 – SudeepShakya May 04 '17 at 11:35
  • Why don't you try this: http://stackoverflow.com/questions/5349362/spring-bean-initialization-with-multiple-arg-method Or overriding that bean: MappingJackso‌​n2HttpMessageConvert‌​er – hoang May 05 '17 at 15:35
  • @hoang where to delare this in java as I am trying to capture this illegal data, I have asked the question which is here https://stackoverflow.com/questions/49676720/post-illegal-unquoted-character-in-json-data-as-a-request?noredirect=1#comment86384077_49676720, can you please help me out. – Vipul Singh Apr 06 '18 at 13:43
  • thanks really useful, the import i used was: `com.fasterxml.jackson.core.JsonParser.Feature;` – Jesús Sánchez Jul 10 '20 at 20:53
  • 1
    what is `mapper`. please, provide context on how it is used – juanchito Apr 30 '21 at 17:20
  • 1
    this is ObjectMapper of Jackson, please see: https://github.com/FasterXML/jackson – hoang May 11 '21 at 03:32
1

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

Daniel Sokolowski
  • 11,982
  • 4
  • 69
  • 55
  • 3
    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](https://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json#comment23184783_42073). – malte Jun 06 '18 at 10:15
0

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
Fabio Mendes Soares
  • 1,357
  • 5
  • 20
  • 30
-1

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

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 23 '21 at 03:14
  • Tab is code 9, not code 10. Please see the existing answers. – Matthew Read Oct 17 '21 at 17:05