52

How to remove the (\)backslash on a string? when using echo json_encode() ?

For example:

<?php
$str = "$(\"#output\").append(\"<p>This is a test!</p>\")";

echo json_encode($str);
?>

note: When you echo $str, there will be no problem... but when you echo out using json_encode(), the (\)backslash will show up.

Is there a way to solve this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ryan
  • 1,783
  • 8
  • 27
  • 42
  • 4
    I ran into the same issues, turned out I was using `json_encode` twice on the same data, that was actually what was adding the backslashes. – eozzy Jun 29 '15 at 02:19
  • JSON requires that quote characters in the data be escaped with backslashes, so if it is doing that it is functioning correctly. But why are you JSON encoding a string? – nnnnnn Feb 16 '16 at 12:50

12 Answers12

91
json_encode($response, JSON_UNESCAPED_SLASHES);
Rijk
  • 11,032
  • 3
  • 30
  • 45
  • And I just want to send it back on ajax. I just want the process done on php rather than on javascript. – Ryan Sep 02 '11 at 11:57
  • 28
    sorry... how is this the "right" answer?? JSON is now used all over the place, not just in Javascript... it's simply a way to serialize data. PHP bungles this (along with so much else) by adding completely superflous escapes to quotes that should not have them! the DATA should have escapes, not the quotes that wrap the data! And as it happens, it's essential to remove those quotes for this to work correctly since escaped quotes is NOT part of JSON. so... how DOES one do it?? – Yevgeny Simkin Jan 12 '13 at 22:54
  • 1
    Agree with the above commenter. There are lots of edge cases in the world of software development. This is not an answer. – cmal Aug 16 '14 at 14:33
  • If you actually put the json with backslashes in jsonlint it will return invalid json. I also do not think this is the correct answer. – hopper May 04 '15 at 08:18
  • 8
    JSON_UNESCAPED_SLASHES is for forward slashes, not quotes. e.g. \/these\/slashes\/are\/the\/target -- not \"these\"quotes\"are\"not\"the\"target – bob Aug 09 '17 at 18:52
  • by using this now I got more `\` :| – Moeez Nov 03 '20 at 03:42
  • I'm so tired with this json_encode in PHP – taufardh Apr 08 '21 at 09:09
55

Since PHP 5.4 there are constants which can be used by json_encode() to format the json reponse how you want.

To remove backslashes use: JSON_UNESCAPED_SLASHES. Like so:

json_encode($response, JSON_UNESCAPED_SLASHES);

View the PHP documentation for more constants and further information:

http://php.net/manual/en/function.json-encode.php

List of JSON constants:

http://php.net/manual/en/json.constants.php

Daveloper87
  • 706
  • 1
  • 8
  • 13
  • Should be available since 5.4? See [here](http://php.net/manual/en/json.constants.php) – aug Sep 29 '15 at 01:44
  • 1
    @joryl The constant should be available since 5.4 so it should work in newer versions too – Daveloper87 Feb 16 '16 at 12:37
  • Thanks for reply :) It should work in 5.5 for example? If not, why did they remove it? And what's alternative in newer then 5.4? – joryl Feb 16 '16 at 12:45
  • @joryl It should work with 5.4+ . Are you seeing a problem using it in a newer version of PHP? – Daveloper87 Feb 17 '16 at 17:28
  • No, I just wonder if I could use it in my future projects. I can't stay on 5.4 version forever :D – joryl Feb 18 '16 at 08:13
  • 9
    I've Just discovered in PHP 5.6 that `JSON_UNESCAPED_SLASHES` works fine for forward slashes but does nothing for backslashes – gawpertron Jun 07 '16 at 13:34
13

If you using PHP 5.2, json_encode just expects only 1 parameter when calling it. This is an alternative to unescape slash of json values:

stripslashes(json_encode($array))

Don't use it if your data is complicated.

Vuong
  • 617
  • 11
  • 26
9

the solution that does work is this:

$str = preg_replace('/\\\"/',"\"", $str);

However you have to be extremely careful here because you need to make sure that all your values have their quotes escaped (which is generally true anyway, but especially so now that you will be stripping all the escapes from PHP's idiotic (and dysfunctional) "helper" functionality of adding unnecessary backslashes in front of all your object ids and values).

So, php, by default, double escapes your values that have a quote in them, so if you have a value of My name is "Joe" in your DB, php will bring this back as My name is \\"Joe\\".

This may or may not be useful to you. If it's not you can then take the extra step of replacing the leading slash there like this:

$str = preg_replace('/\\\\\"/',"\"", $str);

yeah... it's ugly... but it works.

You're then left with something that vaguely resembles actual JSON.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • Great answer! This is what solved it for me when `json_decode` kept giving `null` whenever I tried to decode an escaped JSON string. – silkfire Mar 20 '14 at 11:58
  • This was the easiest solution for me. JSON_UNESCAPED_SLASHES just wasn't working for me, albeit most likely user error. – nodoze Jan 07 '16 at 04:41
7

I just figured out that json_encode does only escape \n if it's used within single quotes.

echo json_encode("Hello World\n");
// results in "Hello World\n"

And

echo json_encode('Hello World\n');
// results in "Hello World\\\n"
totas
  • 10,288
  • 6
  • 35
  • 32
6

Simpler way would be

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);
HungryDB
  • 555
  • 3
  • 11
  • 30
3

As HungryDB said the easier way for do that is:

$mystring = json_encode($my_json,JSON_UNESCAPED_SLASHES);

Have a look at your php version because this parameter has been added in version 5.4.0

json_encode documentation

therealbigpepe
  • 1,489
  • 2
  • 16
  • 23
3

Yes it's possible. Look!

$str = str_replace('\\', '', $str);

But why would you want to?

Yeroon
  • 3,223
  • 2
  • 22
  • 29
2

I use the following to remove the slashes

echo json_decode($mystring, JSON_UNESCAPED_SLASHES);
n4zg
  • 387
  • 2
  • 6
  • 20
1

You do not want to delete it. Because JSON uses double quotes " " for strings, and your one returns

"$(\"#output\").append(\"
This is a test!<\/p>\")"

these backslashes escape these quotes

genesis
  • 50,477
  • 20
  • 96
  • 125
1

The best way you can remove slashes using JSON_UNESCAPED_SLASHES flag inside json_decode()

Example:

echo json_encode($str, JSON_UNESCAPED_SLASHES);

You can use multiple flags in json_encode().

Example:

json_encode($str, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES)
0

All answers with advice to use JSON_UNESCAPED_SLASHES is totally wrong, as this flag doesn't affect backslashes (\) escaping, only usual slashes (/).

Actually, the only option is to replace double backslashes to single backslashes in the resulting string, like this:

$str      = 'some \n text';

$encoded  = json_encode($str, JSON_UNESCAPED_SLASHES);
$replaced = str_replace('\\\\', '\\', $encoded);

echo 'Backslashes doubled: ' . $encoded . '<br>';
echo 'Duplicates replaced: ' . $replaced;

This will output following:

Backslashes doubled: "some \\n text"
Duplicates replaced: "some \n text"
Alex
  • 11
  • 2