0

This is my original json file

data.json (before)

[
{ "thumb": "../userfiles/img/min/m_1.jpg", "image": "../userfiles/img/1.jpg", "title": "Image 1", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_2.jpg", "image": "../userfiles/img/2.jpg", "title": "Image 2", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_3.jpg", "image": "../userfiles/img/3.jpg", "title": "Image 3", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_4.jpg", "image": "../userfiles/img/4.jpg", "title": "Image 4", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_5.jpg", "image": "../userfiles/img/5.jpg", "title": "Image 5", "folder": "Folder 1" }
]

This is my php file that adds an element to the file data.jon

add_json.php

<?
$dir_thumb  = '../userfiles/img/thumb/6_m.jpg';
$dir_img    = '../userfiles/img/6.jpg'; 
$title      = 'img_6';     
$tipo_image = 'news';

$json        = file_get_contents('data.json');
$data        = json_decode($json);

$data[]     = array(
             'thumb'=>  $dir_thumb,
             'image'=>  $dir_img,
             'title'=>  $title,
             'folder'=> $tipo_image         
        );
file_put_contents('data.json', json_encode($data));
?>

This is the resulting file data.json

data.json (after executing the file add_json.php)

[   
{"thumb":"..\/userfiles\/img\/min\/m_1.jpg","image":"..\/userfiles\/img\/1.jpg","title":"Image 1","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_2.jpg","image":"..\/userfiles\/img\/2.jpg","title":"Image 2","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_3.jpg","image":"..\/userfiles\/img\/3.jpg","title":"Image 3","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_4.jpg","image":"..\/userfiles\/img\/4.jpg","title":"Image 4","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_5.jpg","image":"..\/userfiles\/img\/5.jpg","title":"Image 5","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/thumb\/6_m.jpg","image":"..\/userfiles\/img\/6.jpg","title":"img_6","folder":"news"}  
]

What I do to remove backslashes? Thanks for your help

  • 2
    That's just PHP overkill. Don't worry, the JSON is still valid, and the strings have **exactly** the same value they would have without the unnecessary backslashes. – T.J. Crowder Nov 03 '13 at 17:00
  • Or `JSON_UNESCAPED_SLASHES` as mentioned in one of the other dozens of duplicates. – mario Nov 03 '13 at 17:01

3 Answers3

3

You can prevent that with the JSON_UNESCAPED_SLASHES option to json_encode, see the documentation.

Note, though, that it's just PHP overkill. The JSON is still valid, and the strings have exactly the same sequence of characters in them that they would have without the unnecessary escape characters.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You don't need to remove those backslashes, they're just escaping characters.

After reading that JSON formatted string, those sequences of \/ will be automatically converted to /.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
1

What you have is perfectly valid JSON, but if you want to remove it, you can do it with the JSON_UNESCAPED_SLASHES constant:

file_put_contents('data.json', json_encode($data, JSON_UNESCAPED_SLASHES));
Amal Murali
  • 75,622
  • 18
  • 128
  • 150