8

The code I have used:

$val = json_encode(array("test"=>test1,"test2" =>test,"description" => description));
return $val;

The result im getting

{\"test\":\"test1\",\"test2\":\"test\",\"description\":\"description\"}

I need this to fix api

5
  • 2
    From where do u get this output? I mean the php script. Could you show us that code May 15, 2015 at 6:52
  • this is just a sample output.I need to remove the back slashes
    – Aparna
    May 15, 2015 at 6:55
  • 1
    But the point is, where are you getting the top one from? A JSON Decode? An API? Because that could effect the answer May 15, 2015 at 6:59
  • @Aparna If it is the issue of the code or it happens when you assign the json value then its better to solve it at the begining rather than at the end using stripslashes()... May 15, 2015 at 7:01
  • What do you expect? What have you tried to fix it?
    – Nico Haase
    Jun 3, 2020 at 11:34

10 Answers 10

25

Try with stripslashes()

echo stripslashes('{\"test\":{\"test1\":{\"test1\":[{\"test2\":\"1\",\"test3\": \"foo\",\"test4\":\"bar\",\"test5\":\"test7\"}]}}}');

stripslashes()

12

Tried this.

$val = json_encode(array(
  "test"=>'test1',
  "test2" =>'test',
  "description" => 'description'
));

$data = json_decode($val, true, JSON_UNESCAPED_SLASHES);
return $data;

This is the result I received.

enter image description here

3
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review Nov 6, 2018 at 19:20
  • Thanks. I have added more detail for the answer. Nov 7, 2018 at 6:24
  • 1
    Your answer worked for me, Thanks @KajornjitSongsaen
    – Prateek
    Nov 17, 2018 at 16:20
8

In php "stripslashes" function is present using that you can remove backslash.

Link for more details.

Example:

echo $strnew = stripslashes('{\"test\":{\"test1\":{\"test1\":[{\"test2\":\"1\",\"test3\": \"foo\",\"test4\":\"bar\",\"test5\":\"test7\"}]}}}');
7

Use stripslashes() And read stripslashes

<?php
$srt="'{\"test\":{\"test1\":{\"test1\":[{\"test2\":\"1\",\"test3\": \"foo\",\"test4\":\"bar\",\"test5\":\"test7\"}]}}}'
";
echo stripslashes($srt);

OUTPUT

'{"test":{"test1":{"test1":[{"test2":"1","test3": "foo","test4":"bar","test5":"test7"}]}}}'

1
  • 1
    I used a combination of your answer and the following answer - json_decode(stripslashes($val), true, JSON_UNESCAPED_SLASHES); - thank you
    – kronus
    Jan 14, 2019 at 12:59
5

you can use JSON_UNESCAPED_SLASHES

json_encode($yourjson, JSON_UNESCAPED_SLASHES);
4

Use string find and replace function

$str="{"test":{"test1":{"test1":[{"test2":"1","test3": "foo","test4":"bar","test5":"test7"}]}}}";

str_replace("\'","'",$str);
2

Try the following code. It works perfectly fine for me $cha a string with backslashes

$cha = "{\"ashen\":\"143\"}";
$chachi = json_decode($cha,JSON_UNESCAPED_SLASHES);
return $chachi['ashen'];
output: 143
4
  • 1
    Please add some explanation to your answer such that others can learn from it
    – Nico Haase
    Jun 3, 2020 at 11:34
  • 1
    @NicoHaase I don't think there is much to explain. When there are "\" in a JSON object that means it's encoded so, what was done is simply use the pre-built-in PHP method json_decode to decode the JSON Object thus giving a pretty print
    – JPilson
    Jun 18, 2020 at 10:17
  • Please add all such explanation to your answer by editing it. Is there anything special in your code? Or anything else that has not been provided in other answers?
    – Nico Haase
    Jun 18, 2020 at 10:58
  • ....especially, as the OP asked how to remove the slashes, not how to decode the string
    – Nico Haase
    Jun 18, 2020 at 10:59
1

Actually, only Khachornchit Songsaen answer is correct.

stripslashes does not work on unescaping escaped " in json encoded strings inside another json.

es.

{ "key1" :"value1", "key2": "{\"key\":\"Text \\\"text\\\" text\"}" }
  

using json_decode($var, true, JSON_UNESCAPED_SLASHES) does the job correctly.

0

this is the right method when your result is coming in slashes do this

    $data = [
        "message" => '',
        "data" => $product
    ];
    $response[] = $data;
    return $response;

do this it's really work because after 5 day i fund this solution or it is right .

-1

I was facing same issue, it is resolved by using echo and exit;

$response = json_encode(array("test"=>"test1","test2" =>"test","description" => "description"));

echo $response;
exit;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.