Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'U' (code 85)

Ask Question

I've been searching for a solution for hours but without succes. I have a json string which I try to map to my java model, but running the code I get the above exception. JSON String:

{"productOptions" : {"2-bruch Kreuzfalz" : "nein"},"uploadData" : {"20682967" : {"mergedFile" : "C:\Users\userName\IdeaProjects\projectName\target\test-classes\com\flyeralarm\tools\kronos\order\integration\input\DE151886742X01_20150727131135.pdf"}}}

To save time and space I won't post the java model code but if it'll be needed to solve the task I'd do it. I suspect that the error comes because the path to the merged file contains 'C:\Users' where a backslash is followed by the char 'U'. Since it is only a suspect I am not quite sure what to do. Am I doing sth. wrong? How can I solve the error?

UPDATE

If I am right and jackson tries to escape the 'U' in the path, then why is it so? I intentionally put the path into the quotes. Why does then jackson considers the slash as an escape attempt?

The problem is that the original JSON in your question is not valid.

According to the JSON specification, the (BNF) syntax for a string is:

string ::= "" 
           " chars "
chars  ::= char
           char chars
char   ::= any-Unicode-character-except-"-or-\-or-control-character
           \u four-hex-digits 

In English, that means that a backslash in a string is an escape character, and it CANNOT be followed by a capital U.

Because the JSON specification says that it MUST do that. The problem is with your understanding of JSON, not in the behaviour of Jackson.

If you want to include literal backslashes in a JSON string, you need to escape them ... with backslashes; e.g.

    {"mergedFile" : "C:\\Users\\userName\\frobbit.pdf"}

Just tried to change the backslash into a normal slash and it worked. Kinda weird that I am forced to use only one type of slashes but at least it works now. To clarify: Changing

"C:\Users\userName\IdeaProjects\projectName\target\test-classes\com\flyeralarm\tools\kronos\order\integration\input\DE151886742X01_20150727131135.pdf"
"C:/Users/a.eirich/IdeaProjects/kronos/src/test/resources/com/flyeralarm/tools/kronos/order/integration/input/DE151886742X01_20150727131135.pdf"

did the job.

Well, it isn't weird. \ is used to escape the next letter/sign. So \U can cause trouble. Either change the \ to / (like to did here) or use \\ instead. – Tom Jul 29, 2015 at 11:57 What is weird is that you didn't read the JSON specification when you encountered this problem. (OK ... not that weird ... rather common behavior really :-) ) – Stephen C Nov 25, 2016 at 23:19

I know this is a bit late, but with 2 backlashes thrown the same error

With 4 backlashes \\\\ works

I have this record in the MySQL database "{\"mergedFile\" : \"C:\\\\Users\\\\userName\\\\frobbit.pdf\"}"

This is misleading. JSON per se does not require four backslashes. However, if you are getting your JSON text from (for example) a Java or MySQL String literal, then you need to escape the backslashes at two levels. That works out as 4 slashes. But my advice is to try to understand the context and languages ... before you try 4 slashes instead of 2. – Stephen C Jun 24, 2021 at 22:51

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.