Json Tutorial

What is JSON

JSON stands for JavaScript Object Notation. It is data saved in the .json file and consists of a series of key/value pairs.  JSON names require double quotes . JSON is used to transfer the data between the server and the browser. 

Sample Json File To Understand Json File Stracture.

Important Tips For Json Data Structure.

  • When Json Data In {}curley Bracket inside {}curley Bracket Data Is JSon Object
  • When Json Data In []3rd Bracket inside []3rd Bracket Data Is JSon Array
//Example 1: Json Object

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}

//Array In Json Format
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}

//Another Json array Example
[{
   "name" : "Steve",
   "age" :  "29",
   "gender" : "male"

},
{
   "name" : "Peter",
   "age" : "32",
   "gender" : "male"

},
{
   "name" : "Sophie",
   "age" : "27",
   "gender" : "female"
}];

Now Going To Json Opreation To Various Programing Language.

  1. Php Array To Json Array
<?php

// app.php

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>


Convert PHP Object to JSON 

<?php

// app.php

class App {
  
}
$app = new App();
$app->title = 'Harry Potter Game';
$app->price = 20;

$jsonData = json_encode($app);
echo $jsonData;

Convert PHP String to JSON

<?php
$str = "hello AppDividend";
echo json_encode($str);
?>

Convert Multidimensional PHP Array into JSON

<?php
/* 
    multidimensional array initialization
*/
$cars = array(
    array(
        "name"=>"Urus", 
        "type"=>"SUV", 
        "brand"=>"Lamborghini"
    ),
    array(
        "name"=>"Cayenne", 
        "type"=>"SUV", 
        "brand"=>"Porsche"
    ),
    array(
        "name"=>"Bentayga", 
        "type"=>"SUV", 
        "brand"=>"Bentley"
    ),
);

echo json_encode($cars);
?>