json_encode() escaping forward slashes

json_encode() escaping forward slashes

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

Stefan Bogdanescu

Founder & Senior Architect

json_encode() Escaping Forward Slashes: What's Really Happening and Why You Can't Stop It

You are encountering a fundamental aspect of JSON serialization that often trips up developers working with APIs like Instagram's. The behavior you are seeing—where forward slashes (/) in URLs become \/ within the cached file—is not a bug, nor is it an optional setting you have misconfigured. It is the default and required behavior of the json_encode() function as defined by the JSON specification itself.

When you execute $instagrams = json_decode($response)->data; and subsequently run file_put_contents($cache, json_encode($results));, PHP is strictly adhering to RFC 8259. This standard mandates that a forward slash must be escaped with a backslash in any JSON text. While this might seem redundant since the browser or downstream parser can handle an unescaped slash just fine, omitting the escape character technically invalidates the JSON structure according to strict parsers.

The reason json_encode() does not provide a built-in flag to disable this specific escaping is rooted in historical compatibility and data integrity. In the early days of JSON adoption, different parsers handled slashes differently. Some required them to be escaped, while others treated them as literals. To ensure universal interoperability, the standard was tightened to require escaping. PHP followed this lead because json_encode() acts as a serializer responsible for producing valid JSON text. If you were to disable the escaping of forward slashes, your output would technically become invalid JSON according to the current standard.

However, there is a way to generate unescaped slashes if your specific use case demands it, though you must understand the trade-offs involved. The solution lies in modifying how the data is serialized before it hits the file system or by using alternative encoding strategies depending on your version of PHP.

If you are working with newer versions of PHP (specifically 8.4+), you might encounter changes to default behavior regarding json_encode(), but historically, and in most stable environments today, there is no direct flag like JSON_UNESCAPE_SLASHES that applies globally without risking compliance issues. Instead, the common workaround involves preprocessing the data or using a custom serializer if strict RFC compliance isn't a hard requirement for your cache format.

In many caching scenarios, such as storing an array of results in a file, the consuming application often parses the JSON back into memory. When doing so, the consumer will receive the URL as http:\/\/distilleryimage4.instagram.com\/.... While this looks ugly, it is functionally identical to http://distilleryimage4.instagram.com/. The double quotes and escape sequences are merely transport layer metadata that disappear once json_decode() is called on your cache file.

If you strictly need the raw slashes in the file without any escaping—for instance, if you are creating a text-based log rather than a JSON payload—you should consider changing the output format entirely. You might use var_export() or serialize the data with serialize(), though neither produces valid JSON. Alternatively, if you must keep the JSON format but need unescaped slashes, you would have to manually post-process the string after encoding, replacing every instance of \/ back to /.

$json = json_encode($results);
$unescapedJson = str_replace('\/', '/', $json);
file_put_contents($cache, $unescapedJson);

This approach works for files that are not parsed by strict JSON parsers later in the pipeline. However, be aware that this creates non-standard JSON. If any external tool or library attempts to parse this file directly without your intervention, it will fail validation.

If you are building a larger application where JSON consistency is paramount, it is often better to accept the escaped slashes. The performance cost of json_encode() is negligible for typical dataset sizes, and the guarantee of valid JSON output outweighs the aesthetic preference for unescaped characters. If you require unescaped output in your specific context, ensure that every consumer of that cache file also performs a manual unescape step or accepts the escaped format.

For more robust handling of complex data structures without worrying about these low-level formatting details, Laravel provides excellent utilities. If you are already using the framework, consider leveraging json_encode() within Blade templates or controllers where the output is intended for HTTP responses, as the web server handles the transmission correctly regardless of internal escaping nuances.

Our network