{"id":5953,"date":"2015-04-24T09:00:20","date_gmt":"2015-04-24T13:00:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5953"},"modified":"2015-04-23T01:57:48","modified_gmt":"2015-04-23T05:57:48","slug":"parsing-json-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/04\/24\/parsing-json-files\/","title":{"rendered":"Parsing JSON files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33381-jsonlab--a-toolbox-to-encode-decode-json-files\">JSONlab<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1583198\">Qianqian Fang<\/a>.\r\n   <\/p>\r\n   <p>Recently, I've been working quite a bit with <a href=\"http:\/\/en.wikipedia.org\/wiki\/JSON\">JSON<\/a> files. I needed a way to read the files and parse out some content. With structured files like these, it's not terribly difficult\r\n      to find tags and extract out information. But it does take some time. I started to write a program to do this.\r\n   <\/p>\r\n   <p>What was I thinking?! Why re-invent the wheel when, most likely, someone has already done this before. It turns out that there\r\n      are already <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=tag%3A%22json%22\">a few File Exchange entries<\/a> for dealing with JSON formats. Almost any of them would have helped me, but Qianqian's JSONlab caught my eyes. His entry\r\n      directly handled JSON files, as opposed to JSON strings.\r\n   <\/p>\r\n   <p>Let's see it in action. Suppose that there is a JSON file, \"example.json\", whose content is this.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\n {\r\n     \"firstName\": \"John\",\r\n     \"lastName\": \"Smith\",\r\n     \"age\": 25,\r\n     \"address\":\r\n     {\r\n         \"streetAddress\": \"3 Apple Hill Dr\",\r\n         \"city\": \"Natick\",\r\n         \"state\": \"MA\",\r\n         \"postalCode\": \"01760\"\r\n     },\r\n     \"phoneNumber\":\r\n     [\r\n         {\r\n           \"type\": \"home\",\r\n           \"number\": \"123 456 7890\"\r\n         },\r\n         {\r\n           \"type\": \"cell\",\r\n           \"number\": \"098 765 4321\"\r\n         }\r\n     ]\r\n }\r\n<\/pre><p>You can extract the content like this.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">data = loadjson(<span style=\"color: #A020F0\">'example.json'<\/span>);<\/pre><p>Let's take a look.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(data)<\/pre><pre style=\"font-style:oblique\">      firstName: 'John'\r\n       lastName: 'Smith'\r\n            age: 25\r\n        address: [1x1 struct]\r\n    phoneNumber: {[1x1 struct]  [1x1 struct]}\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(data.address)<\/pre><pre style=\"font-style:oblique\">    streetAddress: '3 Apple Hill Dr'\r\n             city: 'Natick'\r\n            state: 'MA'\r\n       postalCode: '01760'\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(data.phoneNumber{1})<\/pre><pre style=\"font-style:oblique\">      type: 'home'\r\n    number: '123 456 7890'\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(data.phoneNumber{2})<\/pre><pre style=\"font-style:oblique\">      type: 'cell'\r\n    number: '098 765 4321'\r\n<\/pre><p>In addition to decoding JSON files into MATLAB structures, the package lets you convert MATLAB structures into JSON format\r\n      strings, as well as deal with binary JSON formats.\r\n   <\/p>\r\n   <p>Thanks Qianqian! You saved me some time with my work!<\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5953#respond\">here<\/a>, or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33381-jsonlab--a-toolbox-to-encode-decode-json-files#comments\">comment<\/a> for Qianqian.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_1ce246d780524ef7a0d0502143299732() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='1ce246d780524ef7a0d0502143299732 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1ce246d780524ef7a0d0502143299732';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2015 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1ce246d780524ef7a0d0502143299732()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p>\r\n<\/div>\r\n<!--\r\n1ce246d780524ef7a0d0502143299732 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33381-jsonlab--a-toolbox-to-encode-decode-json-files JSONlab> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1583198 Qianqian\r\n% Fang>.\r\n%\r\n% Recently, I've been working quite a bit with\r\n% <http:\/\/en.wikipedia.org\/wiki\/JSON JSON> files. I needed a way to read\r\n% the files and parse out some content. With structured files like these,\r\n% it's not terribly difficult to find tags and extract out information. But\r\n% it does take some time. I started to write a program to do this.\r\n%\r\n% What was I thinking?! Why re-invent the wheel when, most likely, someone\r\n% has already done this before. It turns out that there are already\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=tag%3A%22json%22\r\n% a few File Exchange entries> for dealing with JSON formats. Almost any of\r\n% them would have helped me, but Qianqian's JSONlab caught my eyes. His\r\n% entry directly handled JSON files, as opposed to JSON strings.\r\n%\r\n% Let's see it in action. Suppose that there is a JSON file,\r\n% \"example.json\", whose content is this.\r\n%\r\n% <include>example.json<\/include>\r\n%\r\n% You can extract the content like this.\r\n\r\ndata = loadjson('example.json');\r\n\r\n%%\r\n% Let's take a look.\r\n\r\ndisp(data)\r\n\r\n%%\r\ndisp(data.address)\r\n\r\n%%\r\ndisp(data.phoneNumber{1})\r\n\r\n%%\r\ndisp(data.phoneNumber{2})\r\n\r\n%%\r\n% In addition to decoding JSON files into MATLAB structures, the package\r\n% lets you convert MATLAB structures into JSON format strings, as well as\r\n% deal with binary JSON formats.\r\n%\r\n% Thanks Qianqian! You saved me some time with my work!\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5953#respond here>, or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/33381-jsonlab--a-toolbox-to-encode-decode-json-files#comments\r\n% comment> for Qianqian.\r\n\r\n##### SOURCE END ##### 1ce246d780524ef7a0d0502143299732\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is JSONlab by Qianqian Fang.\r\n   \r\n   Recently, I've been working quite a bit with JSON files. I needed a way to read the files and parse out some content. With structured... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/04\/24\/parsing-json-files\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,28],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5953"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5953"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5953\/revisions"}],"predecessor-version":[{"id":5958,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5953\/revisions\/5958"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}