{"id":169,"date":"2020-09-04T05:47:11","date_gmt":"2020-09-04T05:47:11","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=169"},"modified":"2024-02-08T08:20:53","modified_gmt":"2024-02-08T13:20:53","slug":"quick-introduction-to-json","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/quick-introduction-to-json\/","title":{"rendered":"Quick Introduction to JSON"},"content":{"rendered":"\n<p>JavaScript Object Notation (JSON) is a lightweight standard for data interchange. It offers several advantages over XML for data interchange such as it can be used for serializing and transmitting structured data over a network. JSON is used with JavaScript based applications and is primarily used to transmit data between client and server. It is easier to use than XML.<\/p>\n\n\n\n<p>JSON Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n \"car\": &#91;\n {\n   \"make\":\"Toyota\",\n   \"model\":\"Corolla:\n },\n {\n   \"make\":\"Honda\",\n   \"model\":\"Civic:\n }]\n}<\/code><\/pre>\n\n\n\n<p>XML Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;cars&gt;\n &lt;car&gt;\n   &lt;make&gt;Toyota&lt;\/make&gt;\n   &lt;model&gt;Corolla&lt;\/model&gt;\n &lt;\/car&gt;\n &lt;car&gt;\n   &lt;make&gt;Honda&lt;\/make&gt;\n   &lt;model&gt;Civic&lt;\/model&gt;\n &lt;\/car&gt;\n&lt;\/car&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">JSON Example<\/h3>\n\n\n\n<p>In XML, you need to use a parser to access data. JSON data can be converted to JavaScript objects for easy access by standard JavaScript functions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;body&gt;\n&lt;h2&gt;Using JavaScript to create JSON object&lt;\/h2&gt;\n&lt;p id=\"jsontest\"&gt;&lt;\/p&gt;\n&lt;script&gt;\n\/\/ JSON data\nvar text = '{\"make\":\"Toyota\",\"model\":\"Corolla\",\"price\":\"$17,000\"}'\n\/\/ create JSON object\nvar obj = JSON.parse(text);\n\/\/ access data\ndocument.getElementById(\"jsontest\").innerHTML =\n\"Jim bought a \" + obj.make + \" \" + obj.model + \" for \" + obj.price;\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using JavaScript to create JSON object<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Jim bought a Toyota Corolla for $17,000<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>JSON.parse() function converts JSON to an object<\/li><li>document.getElementById() is used to access JavaScript objects<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Working with JSON arrays<\/h3>\n\n\n\n<p>JSON syntax also supports arrays. Following is an example of working with JSON arrays using JavaScript.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;body&gt;\n&lt;p id=\"jsontest\"&gt;&lt;\/p&gt;\n&lt;script&gt;\nvar cars = &#91;\n { \"make\":\"Toyota\",\"model\":\"Corolla\" }, \n { \"make\":\"Honda\",\"model\":\"Civic\" },\n { \"make\":\"Nissan\",\"model\":\"Altima\" },\n];\ndocument.getElementById(\"jsontest\").innerHTML =\n\"List of Makes: &lt;br\/&gt;- \" + cars&#91;0].make + \"&lt;br\/&gt;- \" + cars&#91;1].make + \"&lt;br\/&gt;- \" + cars&#91;2].make;\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List of Makes:\n- Toyota\n- Honda\n- Nissan<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using a JSON file<\/h3>\n\n\n\n<p>JSON code can be stored in a JSON file. This is a more practical way of working with JSON. First create a JSON file and save it. The create an HTML file and access the JSON file from it and access its data.<\/p>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">This following example will only work in a client-server environment<\/span><\/strong><\/p>\n\n\n\n<p>cars.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\n{\n\"make\": \"Toyota\",\n\"model\": \"Corolla\",\n\"url\": \"http:\/\/en.wikipedia.org\/wiki\/Toyota_Corolla\"\n},\n{\n\"make\": \"Honda\",\n\"model\": \"Civic\",\n\"url\": \"http:\/\/en.wikipedia.org\/wiki\/Honda_Civic\"\n},\n{\n\"make\": \"Nissan\",\n\"model\": \"Altima\",\n\"url\": \"http:\/\/en.wikipedia.org\/wiki\/Nissan_Altima\"\n}\n]<\/code><\/pre>\n\n\n\n<p>jsontest.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;body&gt;\n\n&lt;div id=\"jsontest\"&gt;&lt;\/div&gt;\n\n&lt;script&gt;\nvar xmlhttp = new XMLHttpRequest();\nvar url = \"cars.json\";\n\nxmlhttp.onreadystatechange = function() {\n   if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {\n       var myArr = JSON.parse(xmlhttp.responseText);\n       myFunction(myArr);\n   }\n}\nxmlhttp.open(\"GET\", url, true);\nxmlhttp.send();\n\nfunction myFunction(arr) {\n   var out = \"\";\n   var i;\n   for(i = 0; i &lt; arr.length; i++) {\n       out += '&lt;a href=\"' + arr&#91;i].url + '\"&gt;' + \n       arr&#91;i].make + '&lt;\/a&gt;&lt;br&gt;';\n   }\n   document.getElementById(\"jsontest\").innerHTML = out;\n}\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Toyota_Corolla\">Toyota<\/a><br><a href=\"http:\/\/en.wikipedia.org\/wiki\/Honda_Civic\">Honda<\/a><br><a href=\"http:\/\/en.wikipedia.org\/wiki\/Nissan_Altima\">Nissan<\/a><\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">JSON with PHP and MySQL<\/h3>\n\n\n\n<p>JSON works beautifully with PHP and MySQL. Let&#8217;s assume that you have a database table with a list of vehicle makes and models. The following example shows how to extract this information and display it using JSON. Note that this example will only work in a client-server environment.<\/p>\n\n\n\n<p>json.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header(\"Access-Control-Allow-Origin: *\");\nheader(\"Content-Type: application\/json; charset=UTF-8\");\n\n\/\/ database connection\n$host = 'aaaa';\n$db = 'bbbb';\n$user = 'cccc';\n$pass = 'dddd';\n\n$conn = new mysqli($host, $user, $pass, $db);\n$result = $conn-&gt;query(\"SELECT make, model from cars limit 10\");\n\n$out = \"&#91;\";\nwhile($rs = $result-&gt;fetch_array(MYSQLI_ASSOC)) {\n   if ($out != \"&#91;\") {$outp .= \",\";}\n   $out .= '{\"make\":\"'  . $rs&#91;\"make\"] . '\",';\n   $out .= '\"model\":\"'. $rs&#91;\"model\"]     . '\"}'; \n}\n$out .=\"]\";\n$conn-&gt;close();\necho($outp);<\/code><\/pre>\n\n\n\n<p>jsontest.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;body&gt;\n\n&lt;h1&gt;Cars&lt;\/h1&gt;\n&lt;div id=\"jsontest\"&gt;&lt;\/div&gt;\n\n&lt;script&gt;\nvar xmlhttp = new XMLHttpRequest();\nvar url = \"http:\/\/www.mysite.com\/json.php\";\n\nxmlhttp.onreadystatechange=function() {\n   if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {\n       myFunction(xmlhttp.responseText);\n   }\n}\nxmlhttp.open(\"GET\", url, true);\nxmlhttp.send();\n\nfunction myFunction(response) {\n   var arr = JSON.parse(response);\n   var i;\n   var out = \"&lt;table&gt;\";\n\n   for(i = 0; i &lt; arr.length; i++) {\n       out += \"&lt;tr&gt;&lt;td&gt;\" + \narr&#91;i].de +\n \"&lt;\/td&gt;&lt;td&gt;\" +\n arr&#91;i].metaphone +\n \"&lt;\/td&gt;&lt;\/tr&gt;\";\n   }\n   out += \"&lt;\/table&gt;\"\n   document.getElementById(\"jsontest\").innerHTML = out;\n}\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cars<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Toyota<\/td><td>Corolla<\/td><\/tr><tr><td>Honda<\/td><td>Civic<\/td><\/tr><tr><td>Nissan<\/td><td>Altima<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Object Notation (JSON) is a lightweight standard for data interchange. It offers several advantages over XML for data interchange such as it can be used for serializing and transmitting structured data over a network. JSON is used with JavaScript based applications and is primarily used to transmit data between client and server. It is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":546,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[512],"tags":[54,77,27,24,256],"class_list":["post-169","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-misc","tag-javascript","tag-json","tag-mysql","tag-php","tag-quickstart"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":2,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/169\/revisions\/547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/546"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}