Steps

  1. Add string AddType application/json .json to Apache configuation file /etc/apache2/mods-available/mime.conf.
  2. Enable mime module via a2enmod mime.
  3. PHP files don’t add any echo html tags string.
<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";

$myJSON = json_encode($myObj);

echo $myJSON;
?>

Reference