{"id":455,"date":"2016-06-14T20:49:25","date_gmt":"2016-06-15T01:49:25","guid":{"rendered":"http:\/\/lisa.rushworth.us\/?p=455"},"modified":"2016-09-10T22:02:59","modified_gmt":"2016-09-11T03:02:59","slug":"parsing-json-in-javascript","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=455","title":{"rendered":"Parsing JSON In JavaScript"},"content":{"rendered":"<p>We&#8217;ve been trying to get our <a href=\"https:\/\/www.bloomsky.com\/\" target=\"_blank\">BloomSky<\/a> data parsed and reflected in OpenHAB &#8212; we can automatically turn the lights on when there is motion *and* the luminescence is lower than some desired value.\u00a0 Bloomsky has an API which allows us to retrieve JSON formatted data from our weather station. I never worked with JSON before &#8211; I&#8217;d heard the term, but didn&#8217;t actually know what it was &#8230; but I needed to parse it in a JavaScript transform. Does JavaScript do JSON? D&#8217;oh! Turns out JSON is an abbreviation for JavaScript Object Notation, and JavaScript parses JSON data <em>really<\/em> well.<\/p>\n<p>Still need to turn my example web code into a transform that runs from OpenHAB, but getting values out of a JSON formatted string is as easy as using the &#8220;parse&#8221; function:<\/p>\n<pre>&lt;html&gt;\r\n\t  &lt;head&gt;\r\n\t    &lt;script&gt;\r\n\t      function parseMyData() {\r\n\t\tvar input = '{\"DeviceID\":\"83237E\",\"LAT\":41.226644299999997,\"LON\":-81.7224322,\"ALT\":292.78720092773438,\"UTC\":-4,\"DST\":1,\"Searchable\":true,\"RegisterTime\":1464494138,\"CityName\":\"Hinckley\",\"StreetName\":\"Bellus Road\",\"FullAddress\":\"Bellus Road, Hinckley, Ohio, US\",\"DeviceName\":\"Buzzard Cam 01\",\"BoundedPoint\":null,\"NumOfFollowers\":5,\"Data\":{\"Temperature\":80.528000000000006,\"ImageURL\":\"http:\/\/storage.googleapis.com\/bloomsky-img\/eaB1rJytnZSmm5y3qJ1krJqwmJmtoJU=.jpg\",\"Humidity\":50,\"Night\":false,\"ImageTS\":1465938980,\"Luminance\":3445,\"TS\":1465938980,\"Rain\":false,\"Pressure\":29.087148500000001,\"Voltage\":2613,\"UVIndex\":\"1\"},\"Point\":{},\"VideoList\":[\"http:\/\/storage.googleapis.com\/bloomsky-video\/eaB1rJytnZSmm5y3_-4_2016-06-09.mp4\",\"http:\/\/storage.googleapis.com\/bloomsky-video\/eaB1rJytnZSmm5y3_-4_2016-06-10.mp4\",\"http:\/\/storage.googleapis.com\/bloomsky-video\/eaB1rJytnZSmm5y3_-4_2016-06-11.mp4\",\"http:\/\/storage.googleapis.com\/bloomsky-video\/eaB1rJytnZSmm5y3_-4_2016-06-12.mp4\",\"http:\/\/storage.googleapis.com\/bloomsky-video\/eaB1rJytnZSmm5y3_-4_2016-06-13.mp4\"],\"NumOfFavorites\":0}'\r\n\r\n\t\tvar jsonOfInput = JSON.parse(input);\r\n\r\n\t\tdocument.write(\"&lt;P&gt;Device ID is: \" + jsonOfInput.DeviceID + \"&lt;\/P&gt;\");\r\n\t\tdocument.write(\"&lt;P&gt;Temp is: \" + jsonOfInput.Data.Temperature + \"&lt;\/P&gt;\");\r\n\t\tdocument.write(\"&lt;P&gt;Luminance is: \" + jsonOfInput.Data.Luminance + \"&lt;\/P&gt;\");\r\n\t      }\r\n\t    &lt;\/script&gt;\r\n\t  &lt;\/head&gt;\r\n\t  &lt;body&gt;\r\n\t  &lt;h2&gt;Press the button to start&lt;\/h2&gt;\r\n\t    &lt;input type=\"button\" onclick=\"parseMyData()\" value=\"Parse\"\/&gt;\r\n\t  &lt;\/body&gt;\r\n\t&lt;\/html&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve been trying to get our BloomSky data parsed and reflected in OpenHAB &#8212; we can automatically turn the lights on when there is motion *and* the luminescence is lower than some desired value.\u00a0 Bloomsky has an API which allows us to retrieve JSON formatted data from our weather station. I never worked with JSON &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[45,43,44],"class_list":["post-455","post","type-post","status-publish","format-standard","hentry","category-coding","tag-coding","tag-home-automation","tag-openhab"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=455"}],"version-history":[{"count":2,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":457,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/455\/revisions\/457"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}