I am trying to out put a JSON multidimensional array in modx for a rest API. The issue is that the json array starts with [[ and ends with ]] making modx think it is a snippet. How do I avoid this?

The php code is below

$it = 0;
$array_to_send = [[]];
function array_builder ($col1, $col2, $col3, $col4){
Global $it,$array_to_send;
$temp_array = [];
//echo $col1 . " " . $col2 . " " . $col3 . $col4;

//echo json_encode($temp_array);
$array_to_send = $temp_array;

//echo json_encode($array_to_send);
//$it += 1;
//echo $it;
return $array_to_send;

$servername = $_GET["sn"];
$username = $_GET["un"];
$password = $_GET["pw"];
$servername = "[REDACTED]";
$username = "[REDACTED]";
$password = "[REDACTED]";

try {
$conn = new PDO("mysql:host=$servername;dbname=[REDACTED]", $username, $password);
// set the PDO error mode to exception
//echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();

$select_all = $conn->prepare("SELECT * FROM bulk_sms_list;");
$json_data = json_encode($select_all->fetchAll(PDO::FETCH_FUNC, "array_builder"));
$json_data = preg_replace("/\[\[/", "[[-", $json_data);
echo $json_data;
echo json_last_error_msg();
//echo print_r($select_all->fetchAll(PDO::FETCH_ASSOC));
//echo json_encode($select_all->fetchAll(PDO::FETCH_FUNC, "array_builder");
//echo print_r($array_to_send);
//echo json_encode($array_to_send);

}catch (PDOException $e){
echo $e;

The json that Is supposed to be outputted is here:


As you can see with the preg replace on line 45 of the php file I have tried to replace [[ with [[- to make modx ignore the [[ but that doesn’t work. Currently nothing gets outputted because it thinks that data is a snippet. All help is appreciated.

Maybe you can put a space between the 2 brackets.

[ ["75","Data","Data","Data"],["76","Random_data","Random_data","Random_data"] ]

Where and how exactly is this code executed?
Did you create a REST API like described here?


the code is in a php snippet that gets called in a template. IDK if REST api is what to call it. Basically an app goes to the link and retrieves the JSON data that was retrieved from mySQL. Will adding spaces break the JSON decoding.

I believe whitespace is ignored in JSON, but you have to test it.

As an alternative, you can short-circuit and end the MODX processing when you’ve finished your processing by doing:

echo $json_data;

Also if the database you’re connecting to is the same one MODX uses, you can do $modx->query('SELECT * ...') instead of creating your own PDO instance. I’d suggest the second example here for your use case.

1 Like

You were correct JSON did ignore the spaces. We have been struggling with this issue for at least two hours now and it is such a simple and obvious solution.

It is on an external vm. Is the @session_write_close(); way better than adding spaces. It would prevent me from having to do pregreplace.

Stopping it from processing the JSON further does seem like a safer approach than regex. There might be some more [[s elsewhere in your results at some point otherwise - maybe not today, just at some point in the future when it randomly breaks…

I’ll go ahead and implement it today rather than 1 year down the road when im unavailable. Thank you for your fast responses and good help.