What is JSON
- JSON stands for JavaScript Object Notation and subset of JavaScript Programming Language.
- JSON is lightweight data interchange format.
- JSON is language independent and familiar to any programming language including C, C++, JAVA, C#, PHP.
- JSON is "self-describing" and easy to understand.
Structure of JSON
- JSON Object: A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
Ex:- {"name":"Divya", "Stream":"CS"}
- JSON Array: An ordered list of values. In most languages,this is realized as an array, vector, list, or sequence.
An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
Ex: {"Divya", "CS"}.
Now here, I am explanning that how to fetch data from MySQL database and show in JSON format:
Step 1: Create register table in MySQL database

Step 2: Now write the code in PHP file:
- <?php
- $username="root";
- $password="";
- $hostname = "localhost";
- //connection string with database
- $dbhandle = mysql_connect($hostname, $username, $password)
- or die("Unable to connect to MySQL");
- echo "Connected to MySQL<br>";
- // connect with database
- $selected = mysql_select_db("abc",$dbhandle)
- or die("Could not select examples");
- //query fire
- $result = mysql_query("SELECT * FROM register");
- $json_response = array();
- // fetch data in array format
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
- // Fetch data of Fname Column and store in array of row_array
- $row_array['Fname'] = $row['Fname'];
- //push the values in the array
- array_push($json_response,$row_array);
- }
- //
- echo json_encode($json_response);
- ?>
- json_encode() - Returns the JSON representation of a value.


Preeti DashPosted Jun 4, 2018, 12:52 AM
How do you run the program? when I type that url, "the site can't be reached" appears. and what IDE do you use? I am using eclipse
Edited LukmenPosted Mar 22, 2018, 11:08 AM
You are my savior, thank you
miral goyaniPosted Jan 22, 2016, 2:19 AM
table(st_id,st_name,st_class) and table2(bk_id,author,isbn,st_id) with foreign key to how generate json data
Lalit JaiminiPosted Jul 23, 2015, 6:27 AM
thanks
Guest UserPosted Nov 19, 2014, 6:21 AM
thanks for sharing this info. useful for me.
Guest UserPosted Nov 19, 2014, 6:21 AM
Hi Divya, i'm well versed with MySql, but what I learnt is that json_encode function is converting array to JSON object. rite?