w3resource logo

:

JSON Tutorial

JSON Example

Description

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.

XML and JSON

JSON Document

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

XML Document

<dogs>
         	  
         	  <dog>
         	  
         	  <category>companion dogs</category>
         	  
         	  <breed>Chihuahua</breed>
         	  
         	  </dog>
         	  
         	  <dog>
         	  
         	  <category>Hounds</category>
         	  
         	  <breed>Foxhound</breed>
         	  
         	  </dog>
  </dogs>

MySQL and JSON

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

MySQL Table

DEMO MySQL TABLE

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

<?php
         	  $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();
         	  if(mysql_num_rows($result)){
         	  while($row=mysql_fetch_row($result)){
         	  $json['emp_info'][]=$row;
         	  }
         	  }
         	  mysql_close($db_name);
         	  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.

http://api.tumblr.com/v2/blog/web-development-and-seo.tumblr.com/info

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":[]}.