PHP – Validate JSON

Here’s what I did to validate if the JSON is valid. I found tutorials on using json_last_error(), but I found this approach to be much simpler.

$arr_license = json_decode($_POST['json_license'], true);
if(empty($arr_license))
  exit('{"ReturnType":"Error", "ReturnCode":"101", "ReturnMessage":"json_license must be a valid JSON string."}');
//-- Proceed from here with valid $arr_license

Advertisements
Posted in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s