0

I am storing my data in the form of json to the database. User can input with quotes, so i am manually adding slashes before the quotes. So my data becomes like this

array:8 [▼
  "buttonText" => "Large Button"
  "campName" => "Large\'s Button Test"
  "buttonSize" => "1"
]

But when i am converting it into json, the json_encode() function automatically added one more slash before my slash as

"{"buttonText":"Large Button","campName":"Large\\'s Button Test","buttonSize":"1"}"

Why the extra slash will be added, is there any option to prevent that

RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
  • please provide more code. what you are experiencing usually happens due to implementing the json encode functions twice – Ben Yitzhaki Jun 27 '17 at 07:26
  • You can take a look at : [Why is json_encode adding backslashes?](https://stackoverflow.com/questions/10314715/why-is-json-encode-adding-backslashes) – Ravi Sachaniya Jun 27 '17 at 07:27
  • 5
    I can't think of any scenario where you need to add slashes manually. I have the impression that you're trying to prevent SQL injection but you're instead corrupting data. – Álvaro González Jun 27 '17 at 07:28
  • 2
    If you want to encode a value with a backslash, the correct JSON encoding for that is a doubled backslash. **Why are you adding backslashes to your data in the first place?!** – deceze Jun 27 '17 at 07:29
  • 1
    @ravisachaniya — That's about forward slashes, not backslashes. – Quentin Jun 27 '17 at 07:30
  • If double quoted problem in query when storing the data into database so you have to use the mysql_real_escape_string($data) or mysqli_real_escape($con,$data) function in php – Bhavin Thummar Jun 27 '17 at 07:47
  • 1
    *"Why the extra slash will be added, is there any option to prevent that?"* -- Do you want to prevent what? To prevent [`json_encode()`](http://php.net/manual/en/function.json-encode.php) create a valid JSON? Go ahead and remove the backslashes as you please. You will come back tomorrow with another question complaining that [`json_decode()`](http://php.net/manual/en/function.json-decode.php) cannot decode your malformed JSONs. – axiac Jun 27 '17 at 07:57
  • @Always This is about escaping *backslashes*! – deceze Jun 27 '17 at 08:22
  • @axiac actually i am getting an array of objects when fetching the data in db, so i am striping the slashes for each object, but that extra slash is creating problem, i have to stripslashes two times – RAUSHAN KUMAR Jun 27 '17 at 09:54
  • @RAUSHANKUMAR Thanks for approving my answer and if you still need to clean all slashes, here is also one my old solution what I post before 3 years ago: http://php.net/manual/en/function.stripslashes.php#114533 – Ivijan Stefan Stipić Jun 27 '17 at 12:06

2 Answers2

1

Why you do like this? Is totaly bad thing adding slashes manualy.

You can just generate JSON using arrays like:

$arr=array();

$arr['buttonText']="Large Button";
$arr['campName']="Large's Button Test";
$arr['buttonSize']=1;

echo json_encode($arr);

Just use json_encode() to store values and json_decode() to get values.

Here is diferent aproach:

$arr=array(
    'buttonText'=>"Large Button",
    'campName'=>"Large's Button Test",
    'buttonSize'=>1,
);

echo json_encode($arr);

JSON ENCODE - Manual

JSON DECODE - Manual

json_encode() adding slashes automaticaly and json_decode() remove it. You don't need to think about that. Just don't worry and be happy.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
1

Extra slashes are added because that's what json_encode will do. See this documentation

In your case if you're encoding data to store in database, you don't need to manually add slashed. json_encode will take care of all escaping.

miken32
  • 42,008
  • 16
  • 111
  • 154
shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50