I'm trying to post json to a txt file but having some problems with my data. Whenever I check my data to be sent in jQuery it all looks fine, but if I print it out in php I see escape slashes and json_decode returns that data as empty. Here's the snippets of code:
jQuery
$.ajax({
type : 'POST',
url : 'update-json.php',
dataType : 'json',
data : {json : JSON.stringify([{'name':'Bob'},{'name':'Tom'}])},
success : function(){
console.log('success');
},
error : function(){
console.log('error');
}
});
PHP
<?php
$json = $_POST['json'];
$entries = json_decode($json);
$file = fopen('data-out.txt','w');
fwrite($file, $entries);
fclose($file);
?>
PHP ECHO $json
[{\"name\":\"Bob\"},{\"name\":\"Tom\"}]
PHP ECHO $entries
//EMPTY
json_decode()
:NULL
is returned if the json cannot be decoded. – Jason McCreary Jan 16 at 18:16