3

I'm trying to save changes to a JSON file using JQuery and PHP, but it seems my PHP script is escaping the characters when it saves out the JSON, meaning I can't read it back in again.

I'm passing the JSON object ('family') to save.php using the following code:

function saveChanges() {
    $.ajax({
        type: "POST",
         url: "save.php",
         data: {
            data: JSON.stringify(family)
         },


         success: function(msg){
             console.log(data);
   }
 });
    }

Then save.php writes the JSON data to armstrong.json with the following code

<?php

$data = $_POST["data"];
echo $data;
$filename = 'armstrong.json';

if (is_writable($filename)) {
    if (!$handle = fopen($filename, "w")) {
         echo "Cannot open file ($filename)";
         exit;
    }

    if (fwrite($handle, parse_json($data)) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
echo "Success, wrote ($data) to file ($filename)";

fclose($handle);

} else {
    echo "The file $filename is not writable";
}

?>

However the file is being written out as follows:

{\"title\":\"Armstrong\",\"description\":\"The Armstrong Family\",\"patriarchID\":\"id1\",\"latestID\":8,\"members\":[{\"name\":\"Grandad\",\"id\":\"id1\",\"children\":[\"id2\",\"id3\"]},{\"name\":\"Dad\",\"id\":\"id2\",\"children\":[\"id4\",\"id5\",\"id6\",\"id7\"]},{\"name\":\"Uncle\",\"id\":\"id3\"},{\"name\":\"Child\",\"id\":\"id4\"},{\"name\":\"Child\",\"id\":\"id5\"},{\"name\":\"Child\",\"id\":\"id6\"},{\"name\":\"Child\",\"id\":\"id7\"},{\"name\":\"a\",\"id\":\"id8\"}]}{\"title\":\"Armstrong\",\"description\":\"The Armstrong Family\",\"patriarchID\":\"id1\",\"latestID\":9,\"members\":[{\"name\":\"Grandad\",\"id\":\"id1\",\"children\":[\"id2\",\"id3\"]},{\"name\":\"Dad\",\"id\":\"id2\",\"children\":[\"id4\",\"id5\",\"id6\",\"id7\"]},{\"name\":\"Uncle\",\"id\":\"id3\"},{\"name\":\"Child\",\"id\":\"id4\"},{\"name\":\"Child\",\"id\":\"id5\"},{\"name\":\"Child\",\"id\":\"id6\"},{\"name\":\"Child\",\"id\":\"id7\"},{\"name\":\"a\",\"id\":\"id8\"},{\"name\":\"a\",\"id\":\"id9\"}]}{\"title\":\"Armstrong\",\"description\":\"The Armstrong Family\",\"patriarchID\":\"id1\",\"latestID\":10,\"members\":[{\"name\":\"Grandad\",\"id\":\"id1\",\"children\":[\"id2\",\"id3\"]},{\"name\":\"Dad\",\"id\":\"id2\",\"children\":[\"id4\",\"id5\",\"id6\",\"id7\"]},{\"name\":\"Uncle\",\"id\":\"id3\"},{\"name\":\"Child\",\"id\":\"id4\"},{\"name\":\"Child\",\"id\":\"id5\"},{\"name\":\"Child\",\"id\":\"id6\"},{\"name\":\"Child\",\"id\":\"id7\"},{\"name\":\"a\",\"id\":\"id8\"},{\"name\":\"a\",\"id\":\"id9\"},{\"name\":\"a\",\"id\":\"id10\"}]}

Any ideas how I can stop it escaping the characters? The JSON file should look like this

{
            "title"         :   "Armstrong",
            "description"   :   "The Armstrong Family",
            "patriarchID"   :   "id1",
            "latestID"      :   7,
            "members"       :   [
                {
                    "name"  :   "Grandad",
                    "id"    :   "id1",
                    "children": ["id2","id3"]
                },
                {
                    "name"  :   "Dad",
                    "id":       "id2",
                    "children": ["id4","id5","id6","id7"]
                },
                {
                    "name"  :   "Uncle",
                    "id"    :   "id3"
                },
                {
                    "name"  :   "Child",
                    "id" :  "id4"
                },
                {
                    "name"  :   "Child",
                    "id"    :   "id5"
                },
                {
                    "name"  :   "Child",
                    "id"    :   "id6"
                },
                {
                    "name"  :   "Child",
                    "id"    :   "id7"
                }

            ]
}
2
  • 1
    looks related: stackoverflow.com/questions/7417987/… Commented Sep 14, 2011 at 18:41
  • What is parse_json in your php code? That's not a built in php function... Commented Sep 14, 2011 at 18:42

3 Answers 3

4

Maybe you have magic quotes turned on in your php.ini. You should turn them off. This would explain the escaping

EDIT - if you need to know more about magic quotes read here. Magic quotes are bad, if you have access to your php.ini you should turn them off

Sign up to request clarification or add additional context in comments.

3 Comments

Sounds interesting, how would I do that, and would it affect anything else?
if you need to know more about magic quotes [read here]php.net/manual/en/security.magicquotes.php. Magic quotes are bad, if you have access to your php.ini you should turn them off
That does seem to have been the problem. Looking into them now, thanks!
1

You have magic_quotes_gpc enabled and the slashes already exist at the time you do $data = $_POST['data'].

See this answer: Slash appended to all my posts

BTW you could replace fopen/fwrite/fclose by file_put_contents

1 Comment

Thanks, hadn't seen that other question but it did the trick!
0

you have to decode your json then encode for simple but ugly example:

<?php
$json = file_get_contents('php://input');
$data = json_decode($json);
$json = json_encode(array($data));

echo $json;

?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.