emacsでJSON Reformat

例えばこういうJSONがあったとして

{"profile_use_background_image":true,"protected":false,"profile_banner_url":"https:\/\/si0.twimg.com\/brand_banners\/twitter\/1323368512\/live","default_profile":false,"lang":"en","profile_text_color":"333333","followers_count":7610082,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1124040897\/at-twitter_normal.png","name":"Twitter","show_all_inline_media":true,"profile_background_image_url":"http:\/\/a1.twimg.com\/profile_background_images\/378245879\/Twitter_1544x2000.png","created_at":"Tue Feb 20 14:35:54 +0000 2007","utc_offset":-28800,"profile_link_color":"038543","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1124040897\/at-twitter_normal.png","description":"Always wondering what's happening. ","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/378245879\/Twitter_1544x2000.png","time_zone":"Pacific Time (US & Canada)","default_profile_image":false,"geo_enabled":true,"notifications":false,"friends_count":823,"profile_background_color":"ACDED6","screen_name":"twitter","status":{"in_reply_to_user_id_str":null,"geo":null,"entities":{"hashtags":[],"urls":[{"expanded_url":"http:\/\/blog.twitter.com\/2012\/02\/post-bowl-twitter-analysis.html","indices":[27,47],"display_url":"blog.twitter.com\/2012\/02\/post-b\u2026","url":"http:\/\/t.co\/OYYSRSew"}],"user_mentions":[],"media":[{"type":"photo","expanded_url":"http:\/\/twitter.com\/twitter\/status\/166629198054690816\/photo\/1","media_url_https":"https:\/\/p.twimg.com\/Ak_8X2-CEAA96Wx.png","indices":[48,68],"display_url":"pic.twitter.com\/M0AtLQVd","sizes":{"small":{"h":126,"w":340,"resize":"fit"},"thumb":{"h":150,"w":150,"resize":"crop"},"medium":{"h":205,"w":555,"resize":"fit"},"large":{"h":205,"w":555,"resize":"fit"}},"id_str":"166629198058885120","id":166629198058885120,"url":"http:\/\/t.co\/M0AtLQVd","media_url":"http:\/\/p.twimg.com\/Ak_8X2-CEAA96Wx.png"}]},"retweet_count":436,"in_reply_to_screen_name":null,"place":null,"created_at":"Mon Feb 06 21:07:52 +0000 2012","contributors":[7694352],"possibly_sensitive":false,"in_reply_to_status_id":null,"in_reply_to_user_id":null,"favorited":false,"truncated":false,"id_str":"166629198054690816","coordinates":null,"source":"web","in_reply_to_status_id_str":null,"retweeted":false,"id":166629198054690816,"text":"Post-Bowl Twitter analysis http:\/\/t.co\/OYYSRSew http:\/\/t.co\/M0AtLQVd"},"following":false,"profile_background_tile":true,"favourites_count":16,"is_translator":false,"profile_sidebar_fill_color":"F6F6F6","location":"San Francisco, CA","id_str":"783214","follow_request_sent":false,"verified":true,"profile_sidebar_border_color":"EEEEEE","url":"http:\/\/blog.twitter.com\/","id":783214,"listed_count":68652,"contributors_enabled":true,"statuses_count":1266}

見づらいので .emacs(.emacs.d/init.el)に1行書いてあげると、

(defun json-prettify (beg end) (interactive "r") (shell-command-on-region beg end "python -m json.tool" nil t))

範囲選択してから M-x json-prettify でキレイに整形してくれます。*1

{
    "contributors_enabled": true, 
    "created_at": "Tue Feb 20 14:35:54 +0000 2007", 
    "default_profile": false, 
    "default_profile_image": false, 
    "description": "Always wondering what's happening. ", 
    "favourites_count": 16, 
    "follow_request_sent": false, 
    "followers_count": 7610082, 
    "following": false, 
    "friends_count": 823, 
    "geo_enabled": true, 
    "id": 783214, 
    "id_str": "783214", 
    "is_translator": false, 
    "lang": "en", 
    "listed_count": 68652, 
    "location": "San Francisco, CA", 
    "name": "Twitter", 
    "notifications": false, 
    "profile_background_color": "ACDED6", 
    "profile_background_image_url": "http://a1.twimg.com/profile_background_images/378245879/Twitter_1544x2000.png", 
    "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/378245879/Twitter_1544x2000.png", 
    "profile_background_tile": true, 
    "profile_banner_url": "https://si0.twimg.com/brand_banners/twitter/1323368512/live", 
    "profile_image_url": "http://a0.twimg.com/profile_images/1124040897/at-twitter_normal.png", 
    "profile_image_url_https": "https://si0.twimg.com/profile_images/1124040897/at-twitter_normal.png", 
    "profile_link_color": "038543", 
    "profile_sidebar_border_color": "EEEEEE", 
    "profile_sidebar_fill_color": "F6F6F6", 
    "profile_text_color": "333333", 
    "profile_use_background_image": true, 
    "protected": false, 
    "screen_name": "twitter", 
    "show_all_inline_media": true, 
    "status": {
        "contributors": [
            7694352
        ], 
        "coordinates": null, 
        "created_at": "Mon Feb 06 21:07:52 +0000 2012", 
        "entities": {
            "hashtags": [], 
            "media": [
                {
                    "display_url": "pic.twitter.com/M0AtLQVd", 
                    "expanded_url": "http://twitter.com/twitter/status/166629198054690816/photo/1", 
                    "id": 166629198058885120, 
                    "id_str": "166629198058885120", 
                    "indices": [
                        48, 
                        68
                    ], 
                    "media_url": "http://p.twimg.com/Ak_8X2-CEAA96Wx.png", 
                    "media_url_https": "https://p.twimg.com/Ak_8X2-CEAA96Wx.png", 
                    "sizes": {
                        "large": {
                            "h": 205, 
                            "resize": "fit", 
                            "w": 555
                        }, 
                        "medium": {
                            "h": 205, 
                            "resize": "fit", 
                            "w": 555
                        }, 
                        "small": {
                            "h": 126, 
                            "resize": "fit", 
                            "w": 340
                        }, 
                        "thumb": {
                            "h": 150, 
                            "resize": "crop", 
                            "w": 150
                        }
                    }, 
                    "type": "photo", 
                    "url": "http://t.co/M0AtLQVd"
                }
            ], 
            "urls": [
                {
                    "display_url": "blog.twitter.com/2012/02/post-b\u2026", 
                    "expanded_url": "http://blog.twitter.com/2012/02/post-bowl-twitter-analysis.html", 
                    "indices": [
                        27, 
                        47
                    ], 
                    "url": "http://t.co/OYYSRSew"
                }
            ], 
            "user_mentions": []
        }, 
        "favorited": false, 
        "geo": null, 
        "id": 166629198054690816, 
        "id_str": "166629198054690816", 
        "in_reply_to_screen_name": null, 
        "in_reply_to_status_id": null, 
        "in_reply_to_status_id_str": null, 
        "in_reply_to_user_id": null, 
        "in_reply_to_user_id_str": null, 
        "place": null, 
        "possibly_sensitive": false, 
        "retweet_count": 436, 
        "retweeted": false, 
        "source": "web", 
        "text": "Post-Bowl Twitter analysis http://t.co/OYYSRSew http://t.co/M0AtLQVd", 
        "truncated": false
    }, 
    "statuses_count": 1266, 
    "time_zone": "Pacific Time (US & Canada)", 
    "url": "http://blog.twitter.com/", 
    "utc_offset": -28800, 
    "verified": true
}

たぶん Python 2.6 以上が入ってないと使えないと思います。

*1:global-set-keyなどで適当なキーにバインドしても良い