{"id":238,"date":"2013-08-27T16:20:02","date_gmt":"2013-08-27T16:20:02","guid":{"rendered":"http:\/\/www.datadocs.org\/beta\/?p=238"},"modified":"2013-08-27T16:20:02","modified_gmt":"2013-08-27T16:20:02","slug":"freds-new-data-format-friend-json","status":"publish","type":"post","link":"http:\/\/www.datadocs.org\/beta\/freds-new-data-format-friend-json\/","title":{"rendered":"FRED&#8217;s new (data-format) friend, JSON"},"content":{"rendered":"<p>It&#8217;s probably clear by now that we here at Data Docs <a href=\"http:\/\/www.datadocs.org\/beta\/getting-to-know-fred-insight-into-the-federal-reserves-economic-data-api\/\" title=\"Getting to know FRED: Insight into the Federal Reserve\u2019s economic data API\">have a lot of love for AL\/FRED<\/a>, the vast <a href=\"http:\/\/research.stlouisfed.org\/fred2\/\" title=\"FRED website\" target=\"_blank\">web-<\/a> and <a href=\"http:\/\/api.stlouisfed.org\/docs\/fred\/\" title=\"FRED API\" target=\"_blank\">API-accessible<\/a> economic data repository run by the <a href=\"http:\/\/www.stlouisfed.org\/\" title=\"St. Louis Fed homepage\" target=\"_blank\">St. Louis Fed<\/a>. <\/p>\n<p>It is fair to say, though, that we never loved all parts of AL\/FRED equally; while the thoroughness, timeliness, and accuracy of its data have always been outstanding, the XML format in which that data was delivered was better suited suited to an earlier, less JavaScript-driven internet. It was a relatively small price to pay, but accessing AL\/FRED API data in a JavaScript application meant always having to write the descriptive but tiresome (and verbose) <span style=\"color:#FFDB29;\">getElementsByTagName()<\/span> commands in your code in order to use the AL\/FRED data. Always, that is, <a href=\"http:\/\/news.research.stlouisfed.org\/2013\/08\/fred-api-now-supports-json-and-tags\/\" title=\"FRED JSON announcement\" target=\"_blank\">until a week ago Monday<\/a>.<br \/>\nData Docs is proud to be an early user of the AL\/FRED API&#8217;s JSON format, which can be accessed by simply adding <span style=\"color:#FFDB29;\">file_type=json<\/span> to any API call. <\/p>\n<h4>Introducing JSON<\/h4>\n<p>So let&#8217;s say your original API call looks something like the below (for a complete guide to AL\/FRED, see our <a href=\"http:\/\/www.datadocs.org\/beta\/getting-to-know-fred-insight-into-the-federal-reserves-economic-data-api\/\" title=\"Getting to know FRED: Insight into the Federal Reserve\u2019s economic data API\">previous blog post<\/a>):<br \/>\n<\/p>\n<pre>http:\/\/api.stlouisfed.org\/fred\/series\/observations?series_id=PAYEMS\r\n&observation_start=2013-01-01&units=chg&api_key=xxxxxxxxxxxxxxxxxx<\/pre>\n<p>Without the new <span style=\"color:#FFDB29;\">file_type<\/span> parameter, the above query will return the monthly non-farm payrolls figures from January, 2013 until the present in an XML format, with the first element describing the data you&#8217;ve asked for, and each of the next containing one month&#8217;s reading, like so:<br \/>\n<\/p>\n<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;\r\n&lt;observations realtime_start=&quot;2013-08-27&quot; realtime_end=&quot;2013-08-27&quot; \r\nobservation_start=&quot;2013-01-01&quot; observation_end=&quot;9999-12-31&quot; units=&quot;chg&quot;\r\noutput_type=&quot;1&quot; <span style=\"color:#FFDB29;\">file_type=&quot;xml&quot;<\/span>\r\norder_by=&quot;observation_date&quot; sort_order=&quot;asc&quot;count=&quot;7&quot; offset=&quot;0&quot; limit=&quot;100000&quot;&gt;\r\n\r\n&lt;observation realtime_start=&quot;2013-08-27&quot; realtime_end=&quot;2013-08-27&quot; \r\ndate=&quot;2013-01-01&quot; value=&quot;148&quot;\/&gt;\r\n.\r\n.\r\n.\r\n&lt;observation realtime_start=&quot;2013-08-27&quot; realtime_end=&quot;2013-08-27&quot;\r\ndate=&quot;2013-07-01&quot; value=&quot;162&quot;\/&gt;\r\n\r\n&lt;\/observations&gt;<\/pre>\n<p>\nJust by adding the new <span style=\"color:#FFDB29;\">file_type=json<\/span> parameter and value anywhere in the original query, however, we get essentially the same data structure in a handy JSON format. So the query becomes:<br \/>\n<\/p>\n<pre>http:\/\/api.stlouisfed.org\/fred\/series\/observations?series_id=PAYEMS\r\n&observation_start=2013-01-01&units=chg<span style=\"color:#FFDB29;\">&file_type=json<\/span>&api_key=xxxxxxxxxxxxxxxxxx<\/pre>\n<p>\nAnd the result becomes:<\/p>\n<pre>\r\n{\r\n    \"realtime_start\": \"2013-08-27\",\r\n    \"realtime_end\": \"2013-08-27\",\r\n    \"observation_start\": \"2013-01-01\",\r\n    \"observation_end\": \"9999-12-31\",\r\n    \"units\": \"chg\",\r\n    \"output_type\": 1,\r\n    <span style=\"color:#FFDB29;\">\"file_type\": \"json\",<\/span>\r\n    \"order_by\": \"observation_date\",\r\n    \"sort_order\": \"asc\",\r\n    \"count\": 7,\r\n    \"offset\": 0,\r\n    \"limit\": 100000,\r\n    \"observations\": [\r\n        {\r\n            \"realtime_start\": \"2013-08-27\",\r\n            \"realtime_end\": \"2013-08-27\",\r\n            \"date\": \"2013-01-01\",\r\n            \"value\": \"148\"\r\n        },\r\n.\r\n.\r\n.\r\n        {\r\n            \"realtime_start\": \"2013-08-27\",\r\n            \"realtime_end\": \"2013-08-27\",\r\n            \"date\": \"2013-07-01\",\r\n            \"value\": \"162\"\r\n        }\r\n    ]\r\n}\r\n<\/pre>\n<p><\/p>\n<h4>Curly Braces instead of Carrots &#8211; So What?<\/h4>\n<p>If you&#8217;re not a JavaScript developer, the difference between the two result sets above probably doesn&#8217;t seem that thrilling. Thanks to good formatting (hat tip to the essential  JSON validation-and-reformat site, <a href=\"http:\/\/jsonlint.org\/\" title=\"JSON Lint\" target=\"_blank\">jsonlint.org<\/a>), you might possibly have found the second result set a bit easier to read. <\/p>\n<p>The big deal is that JSON (which not-coincidentally stands for <span style=\"color:#FFDB29;\">J<\/span>ava<span style=\"color:#FFDB29;\">S<\/span>cript <span style=\"color:#FFDB29;\">O<\/span>bject <span style=\"color:#FFDB29;\">N<\/span>otation) is <em>infinitely<\/em> easier for <em>JavaScript<\/em> to read, too, meaning programmers have to write less code in order to use data in this format. <\/p>\n<p>So, for example, while using the observations from the XML-formatted results would mean writing out <span style=\"color:#FFDB29;\">data.getElementsByTagName(&#8220;observations&#8221;)<\/span>, the same information can be accessed from the JSON format simply by writing <span style=\"color:#FFDB29;\">data.observations<\/span>. And as anyone who&#8217;s every texted &#8220;Trying 2 reach u&#8221; knows, saving even a handful of characters can really add up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s probably clear by now that we here at Data Docs have a lot of love for AL\/FRED, the vast web- and API-accessible economic data repository run by the St. Louis Fed. It is fair to say, though, that we never loved all parts of AL\/FRED equally; while the thoroughness, timeliness, and accuracy of its [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/posts\/238"}],"collection":[{"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":10,"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":262,"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/posts\/238\/revisions\/262"}],"wp:attachment":[{"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.datadocs.org\/beta\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}