{"id":102,"date":"2023-12-18T06:11:43","date_gmt":"2023-12-18T06:11:43","guid":{"rendered":"https:\/\/zerofold.in\/blogs\/?p=102"},"modified":"2023-12-18T12:03:25","modified_gmt":"2023-12-18T06:33:25","slug":"understanding-json-part-2-reading","status":"publish","type":"post","link":"https:\/\/zerofold.in\/blogs\/blog\/2023\/12\/18\/understanding-json-part-2-reading\/","title":{"rendered":"Understanding JSON &#8211; Part 2, Reading"},"content":{"rendered":"\n<p>In Part 1, we understood what a JSON is and how it can be put to work to transfer data effectively.<\/p>\n\n\n\n<p>In this part, we&#8217;ll quickly understand how JSON Array or Objects can be read.<\/p>\n\n\n\n<p>For reading purposes, we&#8217;re going to use JavaScript because it doesn&#8217;t require any specific library or external code to read it. Here is our sample JSON:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\" style=\"border-width:1px\"><code>{\"trainNumber\":12055, \"trainName\":\"DEHRADUN JAN-SHATABDI\"}<\/code><\/pre>\n\n\n\n<p>Using this block of code, we can obtain the values by referring to the key.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"border-width:1px;font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.042), 0.9rem);\"><code>const trainJSON = '{\"trainNumber\":12055, \"trainName\":\"DEHRADUN JAN-SHATABDI\"}';\nconst obj = JSON.parse(trainJSON);\n\nconsole.log(obj.trainName);\nconsole.log(obj.trainNumber);<\/code><\/pre>\n\n\n\n<p>By using the <code>JSON.parse(jsonObject<\/code>), the <code>obj<\/code> object can now directly access the keys of the trainJSON.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\" style=\"border-width:1px\"><code>&gt; DEHRADUN JAN-SHATABDI\n&gt; 12055<\/code><\/pre>\n\n\n\n<p>Below, is an example how to read a JSON Array and further objects in it.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"border-width:1px;font-size:0.8rem\"><code>const json = '&#91;\n{\"trainNumber\":12055, \"trainName\":\"DEHRADUN JAN-SHATABDI\"},{\"trainNumber\":12013, \"trainName\":\"ASR SHATABDI\"},\n{\"trainNumber\":12005, \"trainName\":\"KALKA SHATABDI\"}]';\n\nconst array = JSON.parse(json);\n\n\/\/ Printing entire object\nconsole.log(array&#91;1]);\n\n\/\/ Accessing particular value in an object\nconsole.log(array&#91;2].trainName)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"border-width:1px;font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.042), 0.9rem);\"><code>&gt; Object { trainNumber: 12013, trainName: \"ASR SHATABDI\" }\n&gt; \"KALKA SHATABDI\"<\/code><\/pre>\n\n\n\n<p>Regardless of language, JSON can be read in any. But external dependencies or libraries might be required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part 1, we understood what a JSON is and how it can be put to work to transfer data effectively. In this part, we&#8217;ll quickly understand how JSON Array or Objects can be read. For reading purposes, we&#8217;re going to use JavaScript because it doesn&#8217;t require any specific library or external code to read [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[46],"tags":[45,14,48,40,37,44,43,39,42,41,38,47],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-json","tag-array","tag-data","tag-independent","tag-javascript","tag-json","tag-json-array","tag-json-object","tag-language","tag-notation","tag-object","tag-reading","tag-writing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":7,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts\/102\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}