Spring boot JSON parse error: Unexpected character error

36 Views Asked by At

I have a spring boot application where I get request text and files as form-data

If I send text data in code format, I get this error

JSON parse error: Unexpected character ('b' (code 98)): was expecting comma to separate Object entries
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:406) ~[spring-web-6.0.10.jar:6.0.10]

The request value is like this

{
    "content": "      #backgroundImage {
        border: none;
        height: 100%;
        pointer-events: none;
        position: fixed;
        top: 0;
        visibility: hidden;
        width: 100%;
      }
      [show-background-image] #backgroundImage {
        visibility: visible;
      }
    </style>
  </head>
  <body>
    <iframe id="backgroundImage" src=""></iframe>
    <ntp-app></ntp-app>
    <script type="module" src="new_tab_page.js"></script>
    <link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
    <link rel="stylesheet" href="chrome://theme/colors.css?sets=ui,chrome">
    <link rel="stylesheet" href="shared_vars.css">
  </body>
</html>",
    "title": "test1"
}

At first I got

JSON parse error: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value

But this one is fixed after using

jackson:
  parser:
    allow-unquoted-control-chars: true

in application.yml

But I don't know how to deal with this Unexpected character error

Is client-side responsible to parse JSON type

or

server-side should parse the JSON type data?

1

There are 1 best solutions below

0
chrwahl On BEST ANSWER

The issue is that you have a string with quotes in it (example: var str = "<p class="some">Test</p>"). These " need to be escaped. It could be done like this: var str = "<p class=\"some\">Test</p>" or you could URL encode the string. JavaScript has functions for encoding and decoding: encodeURI() and decodeURI(). And you can do something similar in Spring boot: Guide to Java URL Encoding/Decoding.

Here is an example on how the JSON object is created in JavaScript (don't mind the ${''} -- they are there because JavaScript will try to interpret the string as JavaScript):

let html_str = `<style>#backgroundImage {
        border: none;
        height: 100%;
        pointer-events: none;
        position: fixed;
        top: 0;
        visibility: hidden;
        width: 100%;
      }
      [show-background-image] #backgroundImage {
        visibility: visible;
      }
    </style>
  </head>
  <body>
    <iframe id="backgroundImage" src=""></iframe>
    <ntp-app></ntp-app>
    <scr${''}ipt type="module" src="new_tab_page.js"></scr${''}ipt>
    <link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
    <link rel="stylesheet" href="chrome://theme/colors.css?sets=ui,chrome">
    <link rel="stylesheet" href="shared_vars.css">
  </body>
</html>`;

let json_obj = {"content": encodeURI(html_str), "title": "test1"};

console.log(json_obj);