I'm trying to log some stuff in a JSON file.
An entry looks like this:
{
"bookindId":"30779265",
"timestamp":"1428447744",
"plate":"D-GO9872",
"cityId":"RLD",
"zoneId":"759",
"highDemandZones":[
"402",
"429",
"714",
"715",
"721",
"728",
"732",
"734",
"742",
"756",
"758",
"763"
],
"accountId":"151426",
"date":"20150408",
"formattedDate":"08.04.2015, 01:02 Uhr"
},
But sometimes, highDemandZones is just missing:
{
"bookindId":"30779279",
"timestamp":"1428447762",
"plate":"D-GO120",
"cityId":"RLD",
"zoneId":"759",
"accountId":"151426",
"date":"20150408",
"formattedDate":"08.04.2015, 01:02 Uhr"
},
My problem is that even if there aren't any highDemandZones, it should at least be in there as an empty array. Also I can defenitely say that it shouldn't be empty either. Which leads me to the conclusion that my php code is flawed.
This is the javascript function
App.prototype.trackBooking = function() {
var self = this;
// [... stripped out other code for clarity]
// get high demand zones
var highDemandZones = [];
$.each(this.model.zoneDemands, function(id, details) {
if(details.demand == 'HIGH') {
highDemandZones.push(id);
}
});
// [... stripped out other code for clarity]
var obj = {
bookindId: this.model.user.booking.bookingId,
timestamp: Math.round(Date.now() / 1000),
plate: this.model.user.booking.vehicle.numberPlate,
cityId: this.model.city.id,
zoneId: car.zoneId,
highDemandZones: highDemandZones,
accountId: this.model.user.accountId,
date: todayFormatted,
formattedDate: day + '.' + month + '.' + year + ', ' + hours + ':' + minutes + ' Uhr'
};
return $.ajax({
type: "POST",
dataType: 'json',
url: 'php/ajax.php',
data: {
booking: obj,
action: 'trackBooking'
}
}).success(function(data) {
console.log('TRACKED');
console.log(data);
}).fail(function(a, b, c) {
console.log(a);
console.log(b);
console.log(c);
});
};
This is php function that'll save this obj to a JSON file
function saveBooking($booking) {
// get current bookings
$json = file_get_contents("../reporting/bookings.json");
$bookingsArr = json_decode($json, true);
if(!isset($bookingsArr[$booking['date']])) {
$bookingsArr[$booking['date']] = array();
}
// push new booking into current bookings array
array_push($bookingsArr[$booking['date']], $booking);
// convert to JSON
$jsonString = json_encode($bookingsArr);
// save into JSON file
file_put_contents('../reporting/bookings.json', $jsonString);
// return JSON
echo $jsonString;
}
Do you have any ideas when this could occur? The IDs (the strings inside highDemandZones) never have extended characters like é.
There are defenitely nodes being stripped out even though the array is not empty. The JSON is valid.
$date = date('Ymd'); $phpArrAsString = print_r($booking, true); file_put_contents($date.'.txt', $phpArrAsString.PHP_EOL, FILE_APPEND);Let's see if I can narrow this down to a function