You are on page 1of 7

Json Tutorial

JSON Tutorial for beginners


PHPTPOINT provides deep knowledge of JSON language. This JSON tutorial for beginners not
only helps beginners as well as also helpful for professionals. This JSON tutorial will help you
to learn JSON fundamentals, examples, syntax, array, object, encode, decode, file, date and
date format.

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.

File Name : first_json_program.json


{"employees":[
{"name":"Neetu", "email":"neetu@gmail.com"},
{"name":"Sonu", "email":"sonu3212@gmail.com"},
{"name":"Abhishek", "email":"abhishek@gmail.com"}
]}

Features of JSON

 Simplicity
 Openness
 Self Describing
 Internationalization
 Extensibility
 Interoperability

JSON wth PHP

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 Tutorial Index


Sr.No. Topics
1 JSON Introduction
2 PHP JSON Example
3 Insert JSON data MySQL
4 Getting data from MySQL into JSON using PHP

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.

JSON example defines Students object with an array of three students:

{"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

Inside curly braces write json objects like:

{"Name": "ravi", "Profile":"Student"}

JSON Array

Json array must be written inside square braces. like:

"student":
[
{"Name":"Ravi", "Course":"PHP"},
{"Name":"Abhi", "Course":"JAVA"},
{"Name":"Rexx","Course":"PHP"}
]

PHP JSON Example


JSON Functions

Function Description

json_encode( ) Returns the JSON representation of a value

json_decode( ) Decodes a JSON string.

json_last_error( ) Returns the last error occurred.

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

string json_encode(mixed value,[int options=0]

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

mixed json_decode(string json,[bool assoc = false,[int depth=512]]

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" }

Insert JSON data MySQL


Create Database and Table
//create database
CREATE DATABASE demo;

USE demo;

//create table JSON


CREATE TABLE json(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
firstName CHAR( 50 ) NOT NULL ,
lastName CHAR( 50 ) NOT NULL

Create a JSON file

{"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";

//connect with database demo


$connect= new mysqli($host,$user,$pass,$db) or die("ERROR:could not connect to the
database!!!");

//select all data from json table


$query="select * from json ";
$result=$connect->query($query);

//fetech all data from json table in associative array format and store in $result variable
$array=$result->fetch_assoc();

//Now encode PHP array in JSON string


$json=json_encode($array,true);

//test the json string


var_dump($json);

//create file if not exists


$fo=fopen("myjson.json","w");

//write the json string in file


$fr=fwrite($fo,$json);

?>

//print_r($array);

$query="insert into json values('','$array[FirstName]','$array[LastName]')";

$connect->query($query);

echo "Data Imported Sucessfully from JSON!";


?>

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";

//connect with database demo


$connect= new mysqli($host,$user,$pass,$db) or die("ERROR:could not connect to the
database!!!");

//select all data from json table


$query="select * from json ";
$result=$connect->query($query);

//fetech all data from json table in associative array format and store in $result variable
$array=$result->fetch_assoc();

//Now encode PHP array in JSON string


$json=json_encode($array,true);

//test the json string


var_dump($json);

//create file if not exists


$fo=fopen("myjson.json","w");

//write the json string in file


$fr=fwrite($fo,$json);

?>

You might also like