0

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.

9
  • Spy the communication between js and php, i doubt that the json_decode function is responsible for that. Commented Apr 9, 2015 at 8:38
  • just to clarify: This is happening on our live server - I was not able to reproduce this locally yet. I just see the JSON missing these nodes and our backend data. Commented Apr 9, 2015 at 10:03
  • Then you can trace the input (in a log file) of the json_decode function, to see if it's really responsible for that. Commented Apr 9, 2015 at 10:09
  • 1
    (Or much better, trace the input only if the highDemandZones key is missing) Commented Apr 9, 2015 at 10:11
  • ok I'm logging everything now: $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 Commented Apr 9, 2015 at 10:34

0

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.