JSON Example


This document provides you with some JSON Example. Examples are associated with XML, MySQL, MongoDB and APIs to add more value. Instead of just writing the JSON code as examples, we thought that it would be of more value to you if we co-relate the examples with those.

If you want to read about various formats JSON can be stored with, you may refer JSON Data Structure.

The following is an example of a JSON document. Bellow that. the XML format of the same document is given.

All of the JSON documents used in this document are validated with JSONLint.


JSON Document

  "Dogs": [
  "category": "Companion dogs",
  "name": "Chihuahua"
  "category": "Hounds",
  "name": "Foxhound"

XML Document

         	  <category>companion dogs</category>


The following is a MySQL table. Using the PHP-MySQL code bellow the table, we have created a JSON document.

MySQL Table


PHP MySQL code to extract data from MySQL table and display as JSON.

         	  $host="localhost"; //replace with your hostname
         	  $username="root"; //replace with your username
         	  $password=""; //replace with your password 
         	  $db_name="demo"; //replace with your database
         	  $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
         	  mysql_select_db("$db_name")or die("cannot select DB");
         	  $sql = "select * from emp_info"; //replace emp_info with your table name
         	  $result = mysql_query($sql);
         	  $json = array();
         	  echo json_encode($json);
         	  // please refer to our PHP JSON encode function tutorial for learning json_encode function in detail 

JSON Document

{"emp_info":[["Rameshwar Ghosh","[email protected]"],["Bidhan   Chatterjee","[email protected]"],["Rebati   Banerjee","[email protected]"]]}

MongoDB and JSON

The following example shows how MongoDB stores data in JSON format. We have a table called 'userdetails' and if we run a query to select all the records of
the table, we get data in JSON format.

mongodb json format data

Tumblr API and JSON

tumblr.com is a messaging platform. It offers an API to retrieve data. Here is a simple command to retrieve blog information. For the sake of simplicity, we have kept authentication method out of the scope of this document.


Where "web-development-and-seo" is the blog name from where you want to retrieve data. When the browser is pointed to this URL, we get following output, which is in JSON format.

{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}.

Previous: Validate JSON with JSONLint
Next: Installation of JSON in PHP and PHP json_decode function

Follow us on Facebook and Twitter for latest update.