How to remove backslash on json_encode() function?

How to remove backslash on json_encode() function?

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

Stefan Bogdanescu

Founder & Senior Architect

Why json_encode() Escapes Backslashes and How to Handle It

When you inspect a raw PHP string like "$(\"#output\").append(\"<p>This is a test!</p>\"), it appears perfectly normal in your code editor or when echoed directly via echo $str. However, the moment you pipe that variable into json_encode(), the output suddenly sprouts extra backslashes before characters like quotes and dollar signs. This behavior often confuses developers because the underlying string data hasn't changed; only its representation has shifted.

The root cause lies in how JSON defines escape sequences versus how PHP interprets them. In a standard JSON document, a literal backslash character is represented as \\, while a quote is \". When PHP's json_encode() processes your string, it adheres strictly to the JSON specification. It scans the input for characters that require escaping in a JSON context—specifically quotes, newlines, tabs, and crucially, backslashes themselves. If your source string contains a literal backslash (which it does, as seen in the \" sequences), json_encode() must escape those backslashes to ensure the resulting JSON string is valid. Consequently, every \ in your input becomes \\ in the output.

Consider the specific example where $str holds a jQuery command. The sequence \"#output\" inside the PHP variable represents a double-quote character preceded by a backslash. When you run echo json_encode($str), the function sees that literal backslash and doubles it to satisfy JSON syntax rules. The result is \"\\\\\\"#output\\\\". This isn't a bug; it is the correct serialization of a string that contains escaped characters.

If your goal is to produce a raw HTML snippet or a string meant for direct display in a browser, wrapping it in json_encode() is likely the wrong tool. You are treating data intended for JavaScript as if it were a JSON payload. If you simply need to output the string as-is, stick with echo $str. There is no "removing" the backslashes without altering the fundamental content of the data or changing how that data is being used.

However, there are scenarios where you genuinely want to strip these escape characters from the JSON output, though doing so requires understanding the trade-off between validity and display. If your intent is to generate a string for client-side execution rather than exchanging structured data, you might consider using json_decode() immediately after encoding if you need to "unserialize" it back into a raw format, but this is circular logic that usually indicates a misunderstanding of the data flow. A more robust approach is to adjust your input generation strategy.

Instead of manually escaping quotes in your PHP string (e.g., writing \" when you mean ", depending on your context), ensure you are using raw strings or proper concatenation. If $str is meant to be a piece of HTML, use htmlspecialchars() for safety rather than JSON encoding:

$str = '$("#output").append("<p>This is a test!</p>")';
echo htmlspecialchars($str, ENT_QUOTES, 'UTF-8');

This renders the string safely for HTML without introducing unnecessary JSON escape sequences. Conversely, if you are building an API response and need to return that specific string as part of a JSON object, you must accept that json_encode() will escape the backslashes. You cannot "remove" them while maintaining valid JSON. Attempting to manually strip the backslashes from the encoded output will break the JSON structure, causing parsing errors on the client side.

The only time you should see those extra backslashes is if your input data itself contains them. If your source string is constructed dynamically and includes literal \\ characters that you don't intend, review how you are building that string. For instance, if you are concatenating user input that already contains escaped quotes, json_encode() is doing its job by doubling those escapes again.

For projects built on the Laravel framework, this behavior remains consistent regardless of the version, as it is a core PHP function. However, if you are working with JSON within a larger application and need to manipulate the structure before encoding, ensure you are not double-escaping data at various layers of your architecture. Tools like http://laravelcompany.com often provide resources on handling string serialization in modern PHP stacks, reinforcing that understanding the distinction between internal string representation and serialized output is key to debugging these issues.

Ultimately, the presence of backslashes in json_encode() output is a feature, not a bug. It signifies that the resulting string is a valid JSON value. Removing them manually destroys the validity of the JSON payload, rendering it unusable by any standard parser. The solution is almost always to adjust your input data or choose a different serialization method suited to the specific context in which you are displaying the text.

Our network