Parameters

PHP implements a superset of JSON as specified in the original
» RFC 4627 - it will also encode and
decode scalar types and NULL. RFC 4627 only supports these values when
they are nested inside an array or an object.

Although this superset is consistent with the expanded definition of "JSON
text" in the newer » RFC 7159 (which
aims to supersede RFC 4627) and
» ECMA-404, this may cause
interoperability issues with older JSON parsers that adhere strictly to RFC
4627 when encoding a single scalar value.

assoc

When TRUE, returned objects will be converted into
associative arrays.

depth

User specified recursion depth.

options

Bitmask of JSON decode options. Currently only
JSON_BIGINT_AS_STRING
is supported (default is to cast large integers as floats)

Return Values

Returns the value encoded in json in appropriate
PHP type. Values true, false and
null are returned as TRUE, FALSE and NULL
respectively. NULL is returned if the json cannot
be decoded or if the encoded data is deeper than the recursion limit.

Accessing elements within an object that contain characters not
permitted under PHP's naming convention (e.g. the hyphen) can be
accomplished by encapsulating the element name within braces and the apostrophe.

<?php

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);print $obj->{'foo-bar'}; // 12345

?>

Example #3 common mistakes using json_decode()

<?php

// the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes// single quotes are not valid $bad_json = "{ 'bar': 'baz' }";json_decode($bad_json); // null

// the name must be enclosed in double quotes$bad_json = '{ bar: "baz" }';json_decode($bad_json); // null

If you store your json-string in an utf8-file and read it with file_get_contents, please make sure to strip leading BOM (byte order mark) before decoding it with json_decode. Otherwise json_decode will fail creating an associative array. Instead it will return your data as a string.

First of all, since JSON is not native PHP format or even native JavaScript format, everyone who wants to use JSON wisely should carefuly read official documentation. There is a link to it here, in "Introduction" section. Many questions like "it doesn't recognize my strings" and those like previous one (about zip codes) will drop if you will be attentive!

And second. I've found that there is no good, real working example of how to validate string if it is a JSON or not.
There are two ways to make this: parse input string for yourself using regular expressions or anything else, use json_decode to do it for you.
Parsing for yourself is like writing your own compiler, too difficult.
Just testing result of json_decode is not enough because you should test it with NULL, but valid JSON could be like this 'null' and it will evaluate to NULL. So you should use another function - json_last_error. It will return error code of the last encode/decode operation. If no error occured it will be JSON_ERROR_NONE. So here is the function you should use for testing:
<?php
function isValidJson($strJson) {
json_decode($strJson);
return (json_last_error() === JSON_ERROR_NONE);
}
?>
It's so simple, that there is no need to use it and slow down your script with extra delay for function call. Just do it manualy in you code while working with input data:
<?php
//here is my initial string
$sJson = $_POST['json'];
//try to decode it
$json = json_decode($sJson);
if (json_last_error() === JSON_ERROR_NONE) {
//do something with $json. It's ready to use
} else {
//yep, it's not JSON. Log error or alert someone or do nothing
}
?>

This function will remove trailing commas and encode in utf8, which might solve many people's problems. Someone might want to expand it to also change single quotes to double quotes, and fix other kinds of json breakage.

with two lines you can convert your string from JavaScript toSource() (see http://www.w3schools.com/jsref/jsref_toSource.asp) output format to JSON accepted format. this works with subobjects too!
note: toSource() is part of JavaScript 1.3 but only implemented in Mozilla based javascript engines (not Opera/IE/Safari/Chrome).

My initial problem was to have PHP check a form in case JavaScript was disabled on the client.I fiddled with json_decode for a while before realizing what I really wanted: to be able to initialize the same object in PHP and JavaScript from a common source file.

I ended up writing a tiny parser for a JavaScript object initializer, which is close to - but not the same thing as - a piece of JSON.Among other things, it- recognizes regexes (turning them into PHP strings),- handles C/C++ comments- accepts non-quoted field names.

This parser will accept a superset of real JS object initializer syntax (for instance non-quoted string litterals or improperly formed regexes). Error report and sanity checks are close to non-existent.The whole idea is to share the code among JavaScript and PHP, so the syntactical checks are left to the JS interpreter.

Numbers that cannot be handled by integer seems to become float casted. This can be a problem if you transfer big numbers like facebook ids over JSON. Either you avoid numbers by cast everything to string before JSON.stringify or you have to use number_format if the value bacome a float value.

[NOTE BY danbrown AT php DOT net: This was later re-evaluated and it was determined that an issue did in fact exist, and was patched by members of the Development Team. See http://bugs.php.net/bug.php?id=45989 for details.]

json_decode can not handle string like:\u0014,it will return null,and with the error <?php json_last_error()=JSON_ERROR_CTRL_CHAR ?>according ascii_class from json module, use the next codes to fix the bug:<?php$str = preg_replace_callback('/([\x{0000}-\x{0008}]|[\x{000b}-\x{000c}]|[\x{000E}-\x{001F}])/u', function($sub_match){return '\u00' . dechex(ord($sub_match[1]));},$str);var_dump(json_decode($str));?>

json_decode can not handle string like:\u0014,it will return null,and with the error <?php json_last_error()=JSON_ERROR_CTRL_CHAR ?>according ascii_class from json module, use the next codes to fix the bug:<?php$str = preg_replace_callback('/([\x{0000}-\x{0008}]|[\x{000b}-\x{000c}]|[\x{000E}-\x{001F}])/u', function($sub_match){return '\u00' . dechex(ord($sub_match[1]));},$str);var_dump(json_decode($str));?>