Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Check Valid JSON Data

The following functions will check for and return valid JSON. Generally, json_last_error is best used to return the last error (if any) occurred during the last JSON encoding/decoding. The json_last_error function returns an integer but should be checked by way of the following constants:

Check Valid JSON Data

JSON Error Codes. Source: PHP Manual.

The following will decode JSON and return either the resulting array or false (if the JSON returned an error).

1
<?php 
2
function beliefmedia_check_json($json) {
3
  $result = json_decode($json, true);
4
 return (json_last_error() === JSON_ERROR_NONE) ? $result : false;
5
}

For debugging purposes, it's handy to know the precise error causing grief.

1
<?php 
2
/*
3
 Check Valid JSON Data
4
 http://www.beliefmedia.com/code/php-snippets/check-valid-json
5
*/
6
 
7
function beliefmedia_json_validate($string) {
8
 
9
    /* Check is_string and not empty */
10
    if ( (empty($string)) || (!is_string($string)) ) return false;
11
 
12
    /* Decode the JSON data */
13
    $result = json_decode($string);
14
 
15
    /* Switch and check for possible JSON errors */
16
    switch (json_last_error()) {
17
        case JSON_ERROR_NONE:
18
            $error = ''; // JSON is valid // No error has occurred
19
            break;
20
        case JSON_ERROR_DEPTH:
21
            $error = 'The maximum stack depth has been exceeded.';
22
            break;
23
        case JSON_ERROR_STATE_MISMATCH:
24
            $error = 'Invalid or malformed JSON.';
25
            break;
26
        case JSON_ERROR_CTRL_CHAR:
27
            $error = 'Control character error, possibly incorrectly encoded.';
28
            break;
29
        case JSON_ERROR_SYNTAX:
30
            $error = 'Syntax error, malformed JSON.';
31
            break;
32
        // PHP >= 5.3.3
33
        case JSON_ERROR_UTF8:
34
            $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
35
            break;
36
        // PHP >= 5.5.0
37
        case JSON_ERROR_RECURSION:
38
            $error = 'One or more recursive references in the value to be encoded.';
39
            break;
40
        // PHP >= 5.5.0
41
        case JSON_ERROR_INF_OR_NAN:
42
            $error = 'One or more NAN or INF values in the value to be encoded.';
43
            break;
44
        case JSON_ERROR_UNSUPPORTED_TYPE:
45
            $error = 'A value of a type that cannot be encoded was given.';
46
            break;
47
        default:
48
            $error = 'Unknown JSON error occured.';
49
            break;
50
    }
51
 
52
    if ($error !== '') {
53
        // Throw the Exception or exit
54
        exit($error);
55
    }
56
 
57
  /* JSON OK */
58
  return $result;
59
}

Source: Stackoverflow/Madan Sapkota.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment