LoginSignup
13
11

More than 5 years have passed since last update.

emacsでJSONをフォーマット

Last updated at Posted at 2015-11-09

はじめに

仕事柄よくJSON文字列を見てるのだけど、いちいちコピペしてバッファから取り出してjqかけるのが面倒になってきたのでemacsで一発で変換できたらいいじゃんと思ってやり方を調べてみました。
どんな分野にも先人はいてくださるようで、この方の記事を参考に書きました。

Emacs で JSON Reformat

emacsでJSONをフォーマット

先ほどの参考記事とは別に今回は jq を使いたいので一部変更して下記の定義をemacsの設定ファイルに追加します。

;; for json format
(defun jq-format (beg end)
  (interactive "r")
  (shell-command-on-region beg end "jq ." nil t))

これで選択範囲を指定して jq-format を呼び出せばいい感じにフォーマットしてくれます。
今回はそこまで複雑なことはするつもりがなかったので特に細かい jq のオプションまでは実行したいと思わなかったのでこんなもんでもいいかな、と。

おわりに

jq 便利ですね、今後もしばらくJSONを使う機会は多そう。

13
11
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
11