JSON Tutorial For Beginners
JSON Tutorial For Beginners
With this JSON tutorial, you can learn with examples and with other technologies such as
PHP, Python, jQuery, AJAX and many more, we will also teach you how to convert json to
xml, html, csv, php array and vice versa
It stands for the JavaScript Object Notation. It is used to exchanging and storing the data
from the web-server. It uses the object notation of JavaScript. JavaScript objects can be
change into the JSON and receive its format text into the JavaScript objects.
Advantages of JSON
1. JSON is Faster : These syntaxes are easy to use. We just have to use only -> as a syntax
which provides us an easy parsing of the data and faster execution of the data. It is
syntax is small and light weighted that’s the reason that it executes the response in
the faster way.
2. Schema Support : It has broad range of supported browsers and compatibility with
operating systems so applications made with the coding of JSON do not require so
much effort to make it all browsers compatible. While working, the developer thinks
for the different browsers but it provides that functionality.
3. Server Parsing : JSON from the server side parsing is the important part that developers
want if the parsing will be fast on the server side then only the only user can get the
fast response of their responses so than in this case server-side parsing is the strong
point that indicates us to use the JSON on the server side.
4. Tool for sharing data : For the sharing data of even any size even audio, video and many
more It is the best tool, Because It stores the data in the arrays so data transfer makes
easier. Just because of this reason, It is a superior file format for web APIs and for web
development.
What is JSON
It Stands for JavaScript Object Notation.
It is lightweight data-interchange format.
Easy to read and write than XML.
This is language independent.
It also supports array, object, string, number and values.
Example : with this tutorial, we will give you many examples to understand the topic well.
JSON file should have save with its extension. Let's see a simple example.
Features of JSON
Simplicity
Openness
Self Describing
Internationalization
Extensibility
Interoperability
PHP also permit us to encode and decode JSON with the help of json_encode() and json
decode functions.
1) json_encode()
2) json_decode()
JSON Introduction
Introducing JSON
JSON Stands for JavaScript Object Notation.
JSON is used for exchanging and storing data.
JSON is an easier to use alternative to XML.
JSON is a lightweight data-interchange format.
JSON is language independent.
JSON is "self-describing" and easy to understand.
{"students":[
{"FirstName":"ravi", "Lastname":"srivastva"},
{"FirstName":"Anju", "LastName":"Anju"},
{"FirstName":"Sanjeev", "LastName":"rai"}
]}
The following XML example also defines Students object with 3 students records:
ravi
srivastva
Anju
Anju
Sanjeev
rai
JSON Objects
JSON Array
"student":
[
{"Name":"Ravi", "Course":"PHP"},
{"Name":"Abhi", "Course":"JAVA"},
{"Name":"Rexx","Course":"PHP"}
]
Function Description
PHP json_encode( )
In PHP, json_encode( ) function is used to encode JSON into PHP. In other words we can
say that json_encode( ) function convert PHP variables into JSON String.
Syntax
Ex 1
<?php
$array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($array);
?>
Output {"a":1,"b":2,"c":3,"d":4,"e":5}
Ex 2
<?php
$array = array("Name"=>"Vineet","Profile"=>"Developer","Mobile"=>"9015501897");
echo json_encode($array);
?>
Output {"Name":"Vineet","Profile":"Developer","Mobile":"9015501897"}
PHP json_decode( )
In PHP json_decode( ) function is used to decode JSON into PHP. In other words we can say
that json_decode( ) function convert JSON string into PHP variables.
Syntax
Ex 1
<?php
$json1 = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json1));
var_dump(json_decode($json1, true));
?>
Output object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }array(5) { ["a"] =>
int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }
Ex 2
<?php
$json2 = '{"Name":"Vineet","Profile":"Developer","Mobile":"9015501897"}';
var_dump(json_decode($json2, true));
?>
Output array(3) { ["Name"]=> string(6) "Vineet" ["Profile"]=> string(9) "Developer" ["Mobile"]=> string(10)
"9015501897" }
USE demo;
{"FirstName":"ravi", "LastName":"srivastva"}
PHP Script read JSON file contents and Insert into MySQL
<?php
$host="localhost";
$user="root";
$pass="";
$db="demo";
$connect= new mysqli($host,$user,$pass,$db) or die("ERROR:could not connect to the
database!!!");
$fo=fopen("json_file.json","r");
$fr=fread($fo,filesize("json_file.json"));
$array=json_decode($fr,true);
//To display all values from JSON file Getting data from
MySQL into JSON using PHP
PHP Script for fetching data from mysql and store in JSON file
<?php
$host="localhost";
$user="root";
$pass="";
$db="demo";
//fetech all data from json table in associative array format and store in $result variable
$array=$result->fetch_assoc();
?>
//print_r($array);
$connect->query($query);
//fetech all data from json table in associative array format and store in $result variable
$array=$result->fetch_assoc();
?>