読者です 読者をやめる 読者になる 読者になる

Thanks Driven Life

日々是感謝

Emacs でJSON Reformat (elisp 版)

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

{"completed_in":0.018,"max_id":167805007381544960,"max_id_str":"167805007381544960","next_page":"?page=2&max_id=167805007381544960&q=Tython%20gongodu&rpp=2","page":1,"query":"Tython+gongodu","refresh_url":"?since_id=167805007381544960&q=Tython%20gongodu","results":[{"created_at":"Fri, 10 Feb 2012 03:00:06 +0000","from_user":"gongodu","from_user_id":475906330,"from_user_id_str":"475906330","from_user_name":"MIYAGUNI Wataru","geo":null,"id":167805007381544960,"id_str":"167805007381544960","iso_language_code":"ja","metadata":{"result_type":"recent"},"profile_image_url":"http://a3.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_image_url_https":"https://si0.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","source":"<a href="http://d.hatena.ne.jp/gongoZ/" rel="nofollow">Dubot Is Gongo</a>","text":"\u65E5\u66DC\u65E5\u3082\u3044\u3044\u3051\u3069\u3001Jenkins \u3067 Tython \u5BFE\u8C61\u306E PMD, CPD, Doxygen, Coverage, CppCheck \u3092\u898B\u308C\u308B\u306E\u304B\u30FC\u3002\u304A\u308C\u304C\u9006\u63FA\u308C\u3057\u305F\u304B\u3089\u76F8\u6BBA\u3057\u307E\u3057\u305F","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null},{"created_at":"Fri, 10 Feb 2012 02:30:05 +0000","from_user":"gongodu","from_user_id":475906330,"from_user_id_str":"475906330","from_user_name":"MIYAGUNI Wataru","geo":null,"id":167797455507832834,"id_str":"167797455507832834","iso_language_code":"ja","metadata":{"result_type":"recent"},"profile_image_url":"http://a3.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_image_url_https":"https://si0.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","source":"<a href="http://d.hatena.ne.jp/gongoZ/" rel="nofollow">Dubot Is Gongo</a>","text":"\u8AB0\u304B\u6765\u5E74 1/9 \u306B\u6771\u4EAC\u3067 Tython Conference \u3092","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}],"results_per_page":2,"since_id":0,"since_id_str":"0"}

見づらいので gist からこのファイル を持ってきて load-path 効いてる場所においた後、.emacs.el に1行書いてあげると、

(require 'json-reformat)

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

{
    "since_id_str": "0",
    "since_id": 0,
    "results_per_page": 2,
    "results": [
        {
            "to_user_name": "",
            "to_user_id_str": "",
            "to_user_id": "",
            "to_user": "",
            "text": "日曜日もいいけど、Jenkins で Tython 対象の PMD, CPD, Doxygen, Coverage, CppCheck を見れるのかー。おれが逆揺れしたから相殺しました",
            "source": "<a href="http://d.hatena.ne.jp/gongoZ/" rel="nofollow">Dubot Is Gongo</a>",
            "profile_image_url_https": "https://si0.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
            "profile_image_url": "http://a3.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
            "metadata": {
                "result_type": "recent"
            },
            "iso_language_code": "ja",
            "id_str": "167805007381544960",
            "id": 167805007381544960,
            "geo": "",
            "from_user_name": "MIYAGUNI Wataru",
            "from_user_id_str": "475906330",
            "from_user_id": 475906330,
            "from_user": "gongodu",
            "created_at": "Fri, 10 Feb 2012 03:00:06 +0000"
        },
        {
            "to_user_name": "",
            "to_user_id_str": "",
            "to_user_id": "",
            "to_user": "",
            "text": "誰か来年 1/9 に東京で Tython Conference を",
            "source": "<a href="http://d.hatena.ne.jp/gongoZ/" rel="nofollow">Dubot Is Gongo</a>",
            "profile_image_url_https": "https://si0.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
            "profile_image_url": "http://a3.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
            "metadata": {
                "result_type": "recent"
            },
            "iso_language_code": "ja",
            "id_str": "167797455507832834",
            "id": 167797455507832834,
            "geo": "",
            "from_user_name": "MIYAGUNI Wataru",
            "from_user_id_str": "475906330",
            "from_user_id": 475906330,
            "from_user": "gongodu",
            "created_at": "Fri, 10 Feb 2012 02:30:05 +0000"
        }
    ],
    "refresh_url": "?since_id=167805007381544960&q=Tython%20gongodu",
    "query": "Tython+gongodu",
    "page": 1,
    "next_page": "?page=2&max_id=167805007381544960&q=Tython%20gongodu&rpp=2",
    "max_id_str": "167805007381544960",
    "max_id": 167805007381544960,
    "completed_in": 0.018
}

Python に頼らなくてもできますよ!

元ネタ