PHP JSON

In this PHP tutorial, you will learn all about JSON in PHP. We will discuss in detail the encoding and decoding of JSON in PHP.

What is meant by JSON?

JSON is an abbreviation for JavaScript Object Notation. JSON is a lightweight data transfer format that is simple to understand and create. JSON, like XML, is a text-based format that is simple to write and interpret for both humans and computers; but, unlike XML, JSON data structures consume less bandwidth than their XML counterparts.

JSON can be represented in the following structures:

  •     Object: The Object in JSON is defined as a collection of key/value pairs (i.e. key: value). Each object is separated by a left curly bracket "{" and a right curly bracket "}". A comma "," is used to separate several key/value pairs.
  •     Array: The array in JSON is defined as a list of values in order. An array is denoted by a left bracket "[" and terminated by a right bracket "]". A comma "," is used to separate the values.

The Keys in JSON are always strings, however, values can be a string, integer, true or false, null, or even an object or an array. Strings must be surrounded by double-quotes.
A JSON data structure looks a lot like a PHP array. PHP has built-in functions for encoding and decoding JSON data. These function json_encode() is used to encode data into JSON and json_decode() is used to decode data from JSON, respectively.

How to encode data into JSON in PHP?

In PHP, the json_encode() function is used to encode data into JSON. If successful, this method produces the JSON representation of a value; otherwise, it returns FALSE.
Syntax  


json_encode ( value, options )

Example


$details = array("first_name" => "John", "last_name" => "Doe", "age" => 23);
echo json_encode($details);

Output:


{"first_name":"John","last_name":"Doe","age":23}

For example: if it is not in the key/value pair and we want to use the corresponding index as the key so in such cases we use the JSON_FORCE_OBJECT as the option argument.


$languages = array("PHP", "Python", "Java", "Go", "JavaScript");
echo json_encode($languages, JSON_FORCE_OBJECT);

Output:


{"0":"PHP","1":"Python","2":"Java","3":"Go","4":"JavaScript"}

How to decode data from JSON in PHP?

JSON data decoding is as simple and clear as encoding it. To decode the JSON encoded text into an appropriate PHP data type, use the PHP json_decode() function.

Syntax


json_decode ($json, assoc, depth, options);

Example


$details = array("first_name" => "John", "last_name" => "Doe", "age" => 23);
echo json_encode($details);

Output:


object(stdClass)#1 (3) {
  ["first_name"]=>      
  string(4) "John"      
  ["last_name"]=>       
  string(3) "Doe"       
  ["age"]=>
  int(23)
}

The json_decode() method returns an object by default. However, you may give a second argument assoc, which accepts a Boolean value that, when set to true, converts JSON objects into associative arrays. It is set to false by default.
Example


 $json = '{"first_name":"John","last_name":"Doe","age":23}';
var_dump(json_decode($json, TRUE));

Output:


array(3) {        
  ["first_name"]=>
  string(4) "John"
  ["last_name"]=> 
  string(3) "Doe" 
  ["age"]=>       
  int(23)
}

Example to access the decoded elements individually


$json = '{"first_name":"John","last_name":"Doe","age":23}';
$data = json_decode($json, TRUE);
echo "First Name is " . $data['first_name'] . "\n";
echo "Last Name is " . $data['last_name'] . "\n";
echo "Age is " . $data['age'];

Output:


First Name is John
Last Name is Doe
Age is 23

Example 2


$json = '{"first_name":"John","last_name":"Doe","age":23}';
$data = json_decode($json);
echo "First Name is " . $data->first_name . "\n";
echo "Last Name is " . $data->last_name . "\n";
echo "Age is " . $data->age;

Output:


First Name is John
Last Name is Doe  
Age is 23