We could use [drupal_json_output](https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_json_output/7) instead of:
```PHP
$json = json_encode($pids);
echo $json;
```