This post covers how to use, encode and decode JSON objects when using PHP.
The functions
- json_encode – Returns the JSON representation – link
- json_decode – Decodes a JSON string – link
- json_last_error_msg – Returns the error string of the last json_encode() or json_decode() call – link
- json_last_error – Returns the last error occurred – link
json_encode()
json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) : string
Returns a string containing the JSON representation of the supplied $value, being the value any type except a PHP resource. For instance, an array, an object… And the value must be UTF-8 encoded. The $options parameter is a bit-mask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_UNESCAPED_LINE_TERMINATORS, JSON_THROW_ON_ERROR (see predefined constants here). And finally, the $depth parameter sets the maximum depth, that must be greater than zero.
json_decode()
json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] ) : mixed
Takes a $json UTF-8 encoded, and converts it into a PHP variable. If the $assoc parameter is true, returned objects are converted into associative arrays. The $depth parameter works like the previous function. Therefore it sets the maximum depth, that must be greater than zero. And finally, the $options parameter is a bit-mask of JSON_BIGINT_AS_STRING, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR.
json_last_error_msg()
json_last_error_msg ( void ) : string
Returns the error STRING of the last json_encode() or json_decode() call.
json_last_error()
json_last_error ( void ) : int
Returns the error CODE of the last json_encode() or json_decode() call.
- JSON_ERROR_NONE – No error
- JSON_ERROR_DEPTH – The maximum stack depth has been exceeded
- JSON_ERROR_STATE_MISMATCH – Invalid or malformed JSON
- JSON_ERROR_CTRL_CHAR – Control character error, possibly incorrectly encoded
- JSON_ERROR_SYNTAX – Syntax error
- JSON_ERROR_UTF8 – Malformed UTF-8 characters
- JSON_ERROR_RECURSION – One or more recursive references in the value to be encoded
- JSON_ERROR_INF_OR_NAN – One or more NAN or INF values in the value to be encoded
- JSON_ERROR_UNSUPPORTED_TYPE – A value of a type that cannot be encoded was given
- JSON_ERROR_INVALID_PROPERTY_NAME – A property name that cannot be encoded was given
- JSON_ERROR_UTF16 – Malformed UTF-16 characters
The Examples
PHP Object
From PHP Object to JSON
<?php
$phpObj->name = "Joki";
$phpObj->age = 31;
$phpObj->country = "Spain";
$phpObj->website = "jokiruiz.com";
$phpJSON = json_encode($phpObj);
echo $phpJSON;
{"name":"Joki", "age":31, "country":"Spain", "website":"jokiruiz.com"}
From JSON to PHP Object
<?php
$phpJson = '{"a":1,"b":2,"c":3,"d":4}';
$phpRes = json_decode($phpJson);
var_dump($phpRes);
object(stdClass)#1 (4) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
}
PHP Array
From PHP Array to JSON
<?php
$phpArr = array("Joki", "Mary", "Luca", "Daniel");
$phpJSON = json_encode($phpArr);
echo $phpJSON;
["Joki","Mary","Luca","Daniel"]
From JSON to PHP Array
<?php
$phpJson = '{"a":1,"b":2,"c":3,"d":4}';
$phpRes = json_decode($phpJson, true);
var_dump($phpRes);
array(4) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
}