{"id":354,"date":"2011-11-16T15:25:00","date_gmt":"2011-11-16T20:25:00","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=354"},"modified":"2011-12-26T12:06:31","modified_gmt":"2011-12-26T17:06:31","slug":"beautifying-json-in-emacs","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=354","title":{"rendered":"Beautifying JSON In Emacs"},"content":{"rendered":"\n<p>I really like <a href=\"http:\/\/www.json.org\/\">JSON<\/a>. It&#8217;s the perfect mechanism for serializing data; much better, in my opinion, than the typical XML solution. That said, I don&#8217;t use it enough to justify installing one of the really nice packages that deal with it such as Steve Yegge&#8217;s <a href=\"http:\/\/code.google.com\/p\/js2-mode\/\">js2-mode<\/a> or Edward O&#8217;Connor&#8217;s <a href=\"http:\/\/cvs.savannah.gnu.org\/viewvc\/*checkout*\/emacs\/lisp\/json.el?root=emacs\">json.el<\/a>. Still, I thought it would be nice to have an Emacs utility that could transform JSON into a standard, readable format. <\/p>\n<p> The other day I came across a post in Richard Ketteierij&#8217;s <a href=\"http:\/\/richardlog.com\/\">richardlog<\/a> blog about <a href=\"http:\/\/richardlog.com\/post\/12743073497\/pretty-printing-json-and-xml-on-mac-osx\">pretty-printing JSON and XML on Mac OSX<\/a>. His post had nothing to do with Emacs; it was merely a way of writing reformatted JSON to a file. It occurred to me that this was just what I needed for the occasional lightweight reformatting of JSON. It should work for any system with Python 2.6 or greater installed. <\/p>\n<p> It turns out that the Python distribution comes with a <code>json.tool<\/code> module that does the work of reformating JSON. For example, here <\/p>\n<pre class=\"src src-json\">{<span style=\"color: #8b2252;\">\"father\"<\/span>:{<span style=\"color: #8b2252;\">\"name\"<\/span>:<span style=\"color: #8b2252;\">\"John Smith\"<\/span>, <span style=\"color: #8b2252;\">\"age\"<\/span>:45, <span style=\"color: #8b2252;\">\"employer\"<\/span>:<span style=\"color: #8b2252;\">\"YoYodyne, inc.\"<\/span>},\r\n<span style=\"color: #8b2252;\">\"mother\"<\/span>:{<span style=\"color: #8b2252;\">\"name\"<\/span>:<span style=\"color: #8b2252;\">\"Wilma Smith\"<\/span>, <span style=\"color: #8b2252;\">\"age\"<\/span>:42},\r\n<span style=\"color: #8b2252;\">\"children\"<\/span>:[{<span style=\"color: #8b2252;\">\"name\"<\/span>:<span style=\"color: #8b2252;\">\"William Smith\"<\/span>, <span style=\"color: #8b2252;\">\"age\"<\/span>:15},\r\n         {<span style=\"color: #8b2252;\">\"name\"<\/span>:<span style=\"color: #8b2252;\">\"Sally Smith\"<\/span>,<span style=\"color: #8b2252;\">\"age\"<\/span>:17}]}\r\n<\/pre>\n<p> is some JSON that I used when I first <a href=\"http:\/\/irrealblog.blogspot.com\/2009\/08\/json-esr-recently-put-up-paper-on-his.html\">wrote about JSON<\/a> back in 2009 on my old blog. It&#8217;s easy to run this through <code>json.tool<\/code> by selecting the code and typing \u3010<span class=\"key\">Ctrl<\/span>+<span class=\"key\">u<\/span> <span class=\"key\">Meta<\/span>+<span class=\"key\">|<\/span>\u3011 <code>python -m json.tool<\/code>. Doing that yields <\/p>\n<pre class=\"src src-json\">{\r\n    <span style=\"color: #8b2252;\">\"children\"<\/span>: [\r\n        {\r\n            <span style=\"color: #8b2252;\">\"age\"<\/span>: 15, \r\n            <span style=\"color: #8b2252;\">\"name\"<\/span>: <span style=\"color: #8b2252;\">\"William Smith\"<\/span>\r\n        }, \r\n        {\r\n            <span style=\"color: #8b2252;\">\"age\"<\/span>: 17, \r\n            <span style=\"color: #8b2252;\">\"name\"<\/span>: <span style=\"color: #8b2252;\">\"Sally Smith\"<\/span>\r\n        }\r\n    ], \r\n    <span style=\"color: #8b2252;\">\"father\"<\/span>: {\r\n        <span style=\"color: #8b2252;\">\"age\"<\/span>: 45, \r\n        <span style=\"color: #8b2252;\">\"employer\"<\/span>: <span style=\"color: #8b2252;\">\"YoYodyne, inc.\"<\/span>, \r\n        <span style=\"color: #8b2252;\">\"name\"<\/span>: <span style=\"color: #8b2252;\">\"John Smith\"<\/span>\r\n    }, \r\n    <span style=\"color: #8b2252;\">\"mother\"<\/span>: {\r\n        <span style=\"color: #8b2252;\">\"age\"<\/span>: 42, \r\n        <span style=\"color: #8b2252;\">\"name\"<\/span>: <span style=\"color: #8b2252;\">\"Wilma Smith\"<\/span>\r\n    }\r\n}\r\n<\/pre>\n<p> which is more readable and standardized in the sense that the items are in alphabetical order. As I said, it&#8217;s a simple and lightweight solution but quite handy if you deal with JSON only occasionally. If you use JSON a lot you probably have one of the packages mentioned above installed already (or should have). <\/p>\n<p> <b>Update<\/b>: <del>an example of<\/del> to get rid of redundant \u201cexample.\u201d <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I really like JSON. It&#8217;s the perfect mechanism for serializing data; much better, in my opinion, than the typical XML solution. That said, I don&#8217;t use it enough to justify installing one of the really nice packages that deal with &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=354\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-354","post","type-post","status-publish","format-standard","hentry","category-general","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354"}],"version-history":[{"count":2,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions\/412"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}