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

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