micha3lbrown
micha3lbrown

Reputation: 85

json_encode php 5.5 vs 5.3 not playing nice with js

Here is the issue:

$locArr is a huge array

Then to pull that information into js I use

var oLocations = <?= json_encode($locArr, JSON_FORCE_OBJECT) ?>;

code produced by oLocations = json_encode($locArr);

var oLocations = $.parseJSON( ,"LastDataBuild":"","Drop24Hour":"NO","Pickup24Hour":"NO","MileageUnit":"MI","AirportIndicator":"0",...

Notice the first comma, this throws

Uncaught SyntaxError: Unexpected token ,

if I wrap everything in quotes so that it evaluates to a string I get

Uncaught SyntaxError: Unexpected token : 

I've tried json_encoding in and outside of the javascript, nothings seems to work.

The hardest part about all of this is that at PHP 5.5 I can use:

json_encode($locArr, JSON_PRETTY_TEXT | JSON_FORCE_OBJECT );

and everything works okay. The console will output a nice OBJECT instead of a string, without any errors.

Has anyone else experienced this or do you have any ideas of other things I can try?

Below is the current state of my code (I have been interchangeably adding/removing a few lines of code. at a time):

Declaring the var in PHP:

    $oLocations = json_encode($locArr, JSON_FORCE_OBJECT);

JS trying to use the array:

var oLocations = JSON.parse(<?= $oLocations ; ?>);
        // while(oLocations.charAt(0) !== '"'){
        //  oLocations = oLocations.substr(1);
        // };
        // oLocations = '{' + oLocations;
        // oLocations = $.parseJSON(oLocations);
        // oLocations = JSON.parse(oLocations);
        // console.log(oLocations);
        // oLocations = $.parseJSON(oLocations);

For this example JS threw this "Uncaught SyntaxError: Unexpected token o " and this is the code its referencing:

var oLocations = $.parseJSON([{"RentalLocationID":"KBR","RentalLocationStatus":"Active","RentalLocationType":"Corporate","RentalLocationName":"KBR HOURS M-F 730-6 SAT 9-1 SUN 10-2","AddressLine1":"8555 JOHN CARPENTER FWY","AddressLine2":"","Latitude":"0","Longitude":"0","AddressCity":"DALLAS","AddressState":"TX","AddressStateName":"TEXAS","AddressZipCode":"75247","AddressCountry":"US","AddressCountryName":"UNITED STATES","PhoneNumber":"214-630-6555","AltPhoneNumber":"","FAXNumber":"","ConfirmMessage":["","","",""], ...

Upvotes: 0

Views: 861

Answers (2)

micha3lbrown
micha3lbrown

Reputation: 85

So the problem was not inside of my PHP or JS. It was inside of Expressionengine. It was picking up the "{" and trying to interpret it using EE codes. In the end we ended up using a Pretty Print alternative function that added breaks to each line pairing of key and value.

Upvotes: 0

Tango Bravo
Tango Bravo

Reputation: 3309

There are issues with json_encode when the array is nested 127+ levels deep.

If you're really dealing with a huge array, that's probably what it is.

https://www.php.net/json_decode#refsect1-function.json-decode-returnvalues

Edit: Also weird that you aren't echoing the initial json_encode out and that it's giving you results.

Upvotes: 1

Related Questions