You can use the inbuilt php function json_encode() http://php.net/manual/en/function.json-encode.php
To encode the results use something like
prepare("SELECT * FROM table");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);