<?php
//JSON PHP Validator function


//--------------------------------------------
function json_validator($data=NULL) {

  if (!empty($data)) {    //check if empty
        @json_decode($data);
        return (json_last_error() === JSON_ERROR_NONE);
  }

  return false;
}
//--------------------------------------------


//Example valid JSON
$JSONData1 = '
{
  "employees":
    {
    "employee":
      [
        {"empId":"1","firstName":"John","lastName":"Coles"}
      ]
    }
}
';


//Example in-valid JSON value
$JSONData2  = '
 {
    {
    "emoloyee": [
      {"empId": "1", "firstName": "John", "lastName": "Coles"}
    ]
  }
}
';


echo "Result for JSONData1: ";
echo (json_validator($JSONData1) ? "JSON is Valid" : "JSON is Not Valid");

echo "</br>";
echo "Result for JSONData2: ";
echo (json_validator($JSONData2) ? "JSON is Valid" : "JSON is Not Valid");

?>

