Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP json encoded string escaping

Tags:

json

php

I need to escape json encoded string.

I have encode below string.

json_encode(['test1' => '','test2' => '','test3' => ''])

It will store in mysql table like,

"{\"test1\":\"\",\"test2\":\"\",\"test3\":\"\"}"

I want to store like,

{"test":"","test2":"","test3":""}

Thank you !

like image 524
Jaydeep Mor Avatar asked Jul 08 '26 16:07

Jaydeep Mor


2 Answers

Try below code

<?php
$json = json_encode(['test1' => '','test2' => '','test3' => '']);
$dbjson = "{\"title\":\"\",\"description\":\"\",\"keywords\":\"\"}";

echo "<pre>";
$value = json_encode(json_decode($dbjson));
print_r($value); // output : {"title":"","description":"","keywords":""}
?>
like image 182
Aman Kumar Avatar answered Jul 10 '26 05:07

Aman Kumar


Add JSON_UNESCAPED_SLASHES as last argument

echo json_encode(['test1' => '','test2' => '','test3' => ''],JSON_UNESCAPED_SLASHES)

Output

{"test1":"","test2":"","test3":""}
like image 34
Abdulla Nilam Avatar answered Jul 10 '26 06:07

Abdulla Nilam