You are on page 1of 29

PHP JSON PARSING

LESSON 2
PHP BUILT-IN FUNCTIONS
Two built-in functions are used in PHP to encode and decode
JSON data. These are:

• json_encode()
• json_decode()
PHP | JSON_ENCODE() FUNCTION
The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object
into JSON representation.
Syntax :
string json_encode( $value, $option, $depth )
• Parameters:
• $value: It is a mandatory parameter which defines the value to be encoded.
• $option: It is optional parameter which defines the Bitmask consisting of JSON_FORCE_OBJECT,
JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS,
JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK,
JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION,
JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS,
JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR.
• $depth: It is optional parameter which sets the maximum depth. Its value must be greater than zero.
• Return Value: This function returns a JSON representation on success or false on failure.
ENCODING JSON DATA IN PHP
Example 1: This example encodes PHP array into JSON representation.

OUTPUT:
EXERCISE 1:
Make a PHP code that will show an output like below.
ENCODING JSON DATA IN PHP
Example 2: This example encodes PHP multidimensional array into JSON
representation.
EXERCISE #2:
Make a PHP code that will show an output like below.
ENCODING JSON DATA IN PHP
Example 3: This example encodes PHP objects into JSON representation.
ENCODING JSON DATA IN PHP
Example 4: In the following example, an array is declared and after encoding
the data will be generated in json format.
EXERCISE #3:
Make a PHP code that will show an output like below.
ENCODING JSON DATA IN PHP
Example 5: In the following example, a array is declared and the values of
array are encoded using JSON_FORCE_OBJECT parameter to generate JSON
data with key-value pairs.
EXERCISE #4:
Make a PHP code that will show an output like below.
ENCODING JSON DATA IN PHP
Example 6: In the following example, an associative array is declared and after
encoding it generates JSON data as key-value pairs without using any parameter.
PHP | JSON_DECODE() FUNCTION
The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It
converts a JSON encoded string into a PHP variable.
Syntax :
json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 )

Parameters: This function accepts four parameters as mentioned above and described below:
• json: It holds the JSON string which need to be decode. It only works with UTF-8 encoded strings.
• assoc: It is a boolean variable. If it is true then objects returned will be converted into associative
arrays.
• depth: It states the recursion depth specified by user.
• options: It includes bitmask of JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING,,
JSON_THROW_ON_ERROR.
• Return values: This function returns the encoded JSON value in appropriate PHP type. If the json
cannot be decoded or if the encoded data is deeper than the recursion limit then it returns NULL.
DECODING JSON DATA IN PHP
Example 1: In the following example, JSON data is assigned in a variable and
PHP json_decode() method is used to read the data in PHP format. 
EXERCISE #5:
Make a PHP code that will show an output like below.
DECODING JSON DATA IN PHP
Example 2: In the following example, marks data with roll number are assigned in
JSON format. If you use optional boolean parameter of json_encode() method then the
output will show the object type with total number of elements. Here, the output shoes
array object with 6 elements.
DECODING JSON DATA IN PHP
Example 3: The following example shows how you can read each element of JSON data as
array index and object property. In the script, $age variable which contains JSON data is decoded
as array and object by using optional true parameter. Two types of output are generated here.
The  elements are parsed by array index in the first part and parsed by objects property in the
second part.
DECODING JSON DATA IN PHP
Example 3: OUTPUT
EXERCISE #6:
Make a PHP code that will show an output like below.
DECODING JSON DATA IN PHP
Example 4: 
DECODING JSON DATA IN PHP
Example 5: 
DECODING JSON DATA IN PHP
Example 6: Using loop is the most efficient way to read array variable. The following example
shows how you can parse JSON data using for loop after decoding. Here, the property names
of JSON data are assigned as array index and property values are assigned as array values.
EXERCISE #7:
Make a PHP code that will show an output like below.
DECODING JSON DATA IN PHP
Example 7: In the above examples, JSON data are parsed from the particular variable but
many times it is required to parse data from JSON file.
DECODING JSON DATA IN PHP
Example 7: You have to
use file_get_contents() method to read
JSON file before decoding the data. In
the following script, data of  JSON file
are printed as array  and in the last part
of the script it is shown the way to read
particular property value of JSON data.
Here, the values of the “Name”
property are printed using for loop.
LESSON 2 - ACTIVITY
Go to this link below to answer:

https://forms.gle/UWwbf4qRQwbKURPe8
THE END

You might also like