What is JSON

JSON stands for JavaScript Object Notation. JSON is a standard data-interchange format which is quick and easy to parse and generate.

PHP has some built-in functions to handle JSON.

PHP json_encode()

PHP json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.

Example

<?php 

    // Declare an array  
    $array = array( 
        "name"=>"abc", 
        "email"=>"abc@gmail.com"
        ); 
   
    // Use json_encode() function 
    echo json_encode($array); 
   
?> 

PHP json_decode()

PHP json_decode() function is used to decode a JSON object into a PHP object or an associative array.

<?php 
  
    // Declare a json string 
    $json = '{"name":"abc", "email":"abc@gmail.com"}'; 

    // decode a string 
    var_dump(json_decode($json, true)); 
  
?> 

PHP Accessing the Decoded Values

<?php
    $jsonobj = '{"name":"abc","email":"abc@gmail.com"}';

    $obj = json_decode($jsonobj);

    echo $obj->name;
    echo $obj->email;
?>