We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37054
    • 93 Posts
    Hey folks,

    I am using the jQuery plugin DataTables on a site I'm working on currently and I want to populate the table it produces with JSON data remotely. I have created custom tables and their associated XPDO classes and they are working correctly.

    I have created a snippet and in that snippet I construct the required array as per the datatables documentation in order to produce the JSON datatables can consume Using print_r, the array looks something like:

    Array
    (
        [sEcho] => 0
        [iTotalRecords] => 2
        [iTotalDisplayRecords] => 2
        [aaData] => Array
            (
                [0] => Array
                    (
                        [0] => value1
                        [1] => value2
                        [2] => value3
                        [3] => value4
                    )
    
                [1] => Array
                    (
                        [0] => value1
                        [1] => value2
                        [2] => value3
                        [3] => value4
                    )
    
            )
    
    )
    


    where the aaData array consists of results pulled from the database.

    The problem is that when I run either PHP's native json_encode or the $modx->toJSON function on this array I get:

    {"sEcho":0,"iTotalRecords":"2","iTotalDisplayRecords":"2","aaData":}
    


    So as you can see, the nested aaData array isn't being encoded.

    Now here's the strange part. If I construct that exact same array in a regular vanilla PHP script and encode it using json_encode, it works correctly. I have checked that my arrays are the same within the snippet and the vanilla script and from inspecting both print_r output and var_dump for them, they look identical.

    PHP Version: 5.3.18
    MODX Version: MODX Revolution 2.2.8-pl (traditional)

    This question has been answered by shamblett. See the first response.

    [ed. note: freelancewebdev last edited this post 13 years, 2 months ago.]
    • discuss.answer
      • 26903
      • 1,336 Posts
      I think your falling foul of Revo stripping tags, your nested array should start with '[' or even '[[', these are stripped by Revo when the page is delivered. I fell foul of this, have a look here : https://forums.modx.com/thread/79688/json-encode-problem#dis-post-438886
        Use MODx, or the cat gets it!
        • 37054
        • 93 Posts
        I've made a snippet for others to reproduce and play with this issue - here's the code:

        <?php
        $output = '';
        $ar = array(
            'foo' => 'bar',
            'fuu' => 'bur',
            'nested' => array(
                array(1,2,3),
                array(4,5,6)
                )
            );
            $par = print_r($ar, true);
            $output .= 'print_r($ar): <br/><pre>'.$par.'</pre><br/>';
            $output .= "toJSON: ".$modx->toJSON($ar).'<br/>';
            $output .= 'json_encode: '.json_encode($ar).'<br/>';
            return $output;
        
          • 37054
          • 93 Posts
          You are exactly right - that was the issue - doh!

          To get around it I merely had to run through the JSON string with a str_replace to change all the '[[''s to '[ ['s and the ']]'s to '] ]'s. This doesn't break the javascript object when datatables is consuming the JSON and gets by the MODX parser.

          Thanks Shamblett!

          Quote from: shamblett at Aug 14, 2013, 05:49 AM
          I think your falling foul of Revo stripping tags, your nested array should start with '[' or even '[[', these are stripped by Revo when the page is delivered. I fell foul of this, have a look here : https://forums.modx.com/thread/79688/json-encode-problem#dis-post-438886
            • 26903
            • 1,336 Posts
            No probs, personally I think a resource with a content type of JSON shouldn't be parsed, i.e. the content as generated by the snippet should just be returned untouched, this would allow AJAX calls to just work and take out any delay involved with parsing the output.

              Use MODx, or the cat gets it!
              • 37054
              • 93 Posts
              Well as Mark pointed out, there are instances where you might want to include the output of parsed tags but I can see an argument for having a switch to enable/disable parsing of output at snippet level - would be non-breaking too if the default was to parse.

              Quote from: shamblett at Aug 14, 2013, 07:22 AM
              No probs, personally I think a resource with a content type of JSON shouldn't be parsed, i.e. the content as generated by the snippet should just be returned untouched, this would allow AJAX calls to just work and take out any delay involved with parsing the output.