SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON

You are encountering this specific error because you are passing a JavaScript object directly to JSON.stringify (implicitly, via the jQuery AJAX data property) and then trying to parse that same serialized string as JSON on the response side. The core issue in your code lies in how the request payload is constructed.

In the line var str = {'message': message,'text': text};, you are creating a native JavaScript object. When this object is passed to the jQuery AJAX call, jQuery automatically serializes it into a query string format (e.g., ?message=hello&text=world) by default if you do not explicitly tell it otherwise. While your dataType is set to "json", which tells jQuery to parse the response as JSON, the server receiving this request sees a standard form-encoded payload or a query string, not raw JSON.

If your backend is configured to expect raw JSON and returns a JSON response based on that assumption, you might be running into a circular dependency of serialization issues, or perhaps the server is echoing back the raw object representation rather than a stringified JSON array or object. However, the most likely culprit for the SyntaxError at "column 2" specifically in the context of appending HTML usually involves how the response content type is handled versus what jQuery.parseJSON expects.

The error message indicates that at the second character of the received string, the parser found something it didn't expect. In JSON, valid characters start with {, [, ", or whitespace followed by those. If your response starts with < (as in an HTML comment or a raw HTML tag) or if there is a BOM (Byte Order Mark), the parser will fail immediately.

To fix this, you must ensure strict separation between client-side objects and server-side data transfer formats:

Proper Serialization on the Client

Instead of letting jQuery guess how to serialize your data, explicitly stringify it. This ensures the payload sent over the network is a valid JSON string, regardless of whether jQuery defaults to form data or not.

var str = { 'message': message, 'text': text };
$.ajax({
    type: "POST",
    url: "api/reply",
    contentType: 'application/json', // Explicitly set content type
    data: JSON.stringify(str),       // Explicitly stringify the object
    dataType: "json",
    cache: false,
    success: function(response) {
        // Ensure response is parsed if it comes as a string
        var respons = typeof response === 'string' ? JSON.parse(response) : response;
        var type = respons.status;
        
        if (type == 'success') {
            // Directly assign the message, avoiding potential HTML escaping issues if not needed
            // but be careful with XSS risks if 'respons.message' comes from a user input source
            $("#messages").append('<div>' + respons.message + '</div>'); 
        } else {
            toastr.error(respons.message);
        }
    }
});

By setting contentType: 'application/json', you signal to the server that you are sending JSON. By using JSON.stringify(str), you guarantee that what leaves your client is a string like {"message":"...", "text":"..."}. This prevents jQuery from converting it into ?message=...&text=....

Handling the Response Correctly

Your code uses jQuery.parseJSON(response) inside the success callback. While convenient, this method is deprecated and removed in newer versions of jQuery 3.x. The modern standard is to rely on the dataType: "json" option, which automatically parses the response for you. If the server returns a string that isn't valid JSON, the error will surface here.

The specific error "unexpected character at line 1 column 2" often points to invisible characters or incorrect Content-Type headers. For instance, if your backend (perhaps running on Laravel) is returning an HTML view instead of a JSON response because the route wasn't configured correctly, the response might look like <html>...</html>. The parser sees < at column 1 and then h at column 2, which breaks the strict JSON syntax requiring { or [.

Server-Side Considerations

If you are building the backend using Laravel, ensuring the controller returns a proper JSON response is critical. You should use the response()->json() helper method rather than just echoing a string or returning a view. This guarantees the correct headers and encoding are set:

return response()->json([
    'status' => 'success',
    'message' => 'Your message here',
]);

If you were to return a raw string that accidentally included HTML tags (like a <div> wrapper) without proper escaping or if the view rendering logic was triggered by mistake, the client-side parser would fail exactly as described.

Debugging Strategy

Before assuming the code is broken, inspect the network tab in your browser's developer tools. Look at the "Response" body of the failed request.

  1. Is it valid JSON? If it contains HTML tags, your server is rendering a view instead of returning JSON.
  2. Are there BOM markers? Sometimes text editors save files with a UTF-8 BOM which appears as  at the start of strings, causing immediate parse failures in strict parsers.

By strictly controlling serialization on the client and ensuring the server returns pure JSON with the correct MIME type, you eliminate the ambiguity that causes these parsing exceptions.

Our network