PHP JSON Encode Decode Functions

PHP JSON Encode Decode Functions

PHP, TUTORIALS, WEB

PHP JSON Encode Decode Functions




In this PHP JSON Encode Decode Functions lesson we are gonna learn how the json_encode()   and json_decode()   functions which are very most important functions in transfering data among the applications from the same database.
Encode Functions – PHP JSON Encode Decode Functions
As we known already, by default the Encode is the process of changing the content form the understandable to the encrypted state. So, the json_encode() function is used to convert the PHP data into the supported JSON formatted string.
In json_encode() functions we can have the following contents as parameters.
Data to be encoded.
Options with JSON encode constants to reflect effects on encoding behavior.
Depth limit for performing recursive encoding with nested levels of input.
Consider the following example for how to convert the array of strings into the json formatted strings,

And in PHP JSON Encode Decode Functions the above code shows the following result,
PHP JSON Encode Decode Functions
The json_encode()   has supports some constants as optional parameters.
JSON_HEX_TAG – Used to encode HTML content by replacing < and > symbol with \u003C and \u003E.
JSON_HEX_AMP – Used to encode data by replacing ampersand symbol (&) with \u0026.
JSON_HEX_APOS – encode apostrophe (‘) with \u0027.
JSON_HEX_QUOT – converts double quotes (“) into \u0022.
JSON_FORCE_OBJECT – Using this, json_encode will return object for given input data except associative array.
JSON_NUMERIC_CHECK – PHP JSON encode function will return numbers as a result of encoding given number with string data type.
JSON_BIGINT_AS_STRING – This constant is used to convert the log integer value as string.
JSON_PRETTY_PRINT – pretty print is used for adding white space with the JSON formatted data.
JSON_UNESCAPED_SLASHES – It prevents from escaping slashes (/).
JSON_UNESCAPED_UNICODE – It prevents from escaping unicode characters.
Notes to Remember
PHP json_encode() is used to covert any type of data except PHP resource data.
While using JSON_FORCE_OBJECT on encoding PHP array value, then each array element will be added with an index even though if input array doesn’t have any index.
Decode Functions – PHP JSON Encode Decode Functions
When we have a encode function then we must have the decode function which can convert again the encrypted content. The json_decode()   is used to JSON encoded data into its original PHP format.
In general this json_decode()   accepts four of the following arguments.
JSON formatted string to be decoded.
Boolean value set based on which an associative array will be returned, if it is true.
depth limit.
options.
The third and fourth arguments are same as we have seen for json_encode(). Default value for depth limit is 512. And, the PHP supported constant for option parameter of this function is, JSON_BIGINT_AS_STRING, which is used to convert long integers into float data.




Consider the following example for decode the above encoded PHP array,

And the result for the above code for the json_decode() function we have the following result. It gets the encoded JSON values and recreate them as per the source.
PHP JSON Encode Decode Functions
These are the basic functions of the JSON which helps us to access our database content among various applications.
We hope this PHP JSON Encode Decode Functions lesson will gives you an basic idea about what is JSON and how to work in that. We will learn a lot more in the upcoming lessons.




Leave a Reply