0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

改行やインデントがないJSON形式をファイルをサクラエディタで見やすくする。

Posted at

概要

タイトル通り。忘備録。
毎回改行とタブを置換機能で整形するのがだるかったので、
サクラエディタのマクロでなんとかならんかなと考えて、マクロをJSで書いてみた。

マクロ作り方、実行方法

以下の内容のjsファイルを作成して、サクラエディタでマクロの読み込み→マクロ実行でOK

// 行数を取得
var cnt = GetLineCount(0);
// 改行を除いた行文字列を取得
var textAll = "";
for (var i = 1; i <= cnt; i++) {
    var str = GetLineStr(i).replace(/\r\n/,"").replace(/\n/,"");
    textAll += str;
}
var editJson = "";
var innerFlg = false;
var intentLv = 0;
var quoteType = [];
var blockType = [];
var preChr = "";
for (var i = 0; i < textAll.length; i++) {
    var prefix = "";
    var suffix = "";
    var c = textAll.substring(i, i + 1);
    if (!innerFlg) {
        if (c == " ") {
            continue;
        }
        if (c == "\"" || c == "'"){
            innerFlg = true;
            quoteType[quoteType.length] = c;
            // JSON または 配列の1つ目の要素の時はスペース1個を追加
            var chkText = editJson.replace(/( |\t|\r\n|\n)/g, "");
            if (chkText.length > 0) {
                var preChar = chkText.substring(chkText.length - 1, chkText.length);
                if (preChar == "[" || preChar == "{") {
                    c = " " + c;
                }
            }
        } else {
            if (c == "{" || c == "[") {
                suffix = "\r\n";
                intentLv = intentLv + 1;
                blockType[blockType.length] = c;
                for (var j = 0; j < intentLv; j++) {
                    suffix = suffix + "\t";
                }
            }
            if (c == "}" || c == "]") {
                prefix = "\r\n";
                suffix = "\r\n";
                intentLv = intentLv - 1;
                blockType.pop(1);
            }
            if (c == ",") {
                prefix = "\r\n";
            }
        }
    } else {
        if (quoteType.length > 0 && c == quoteType[quoteType.length - 1]){
            innerFlg = false;
        }
    }
    if (prefix != "") {
        for (var j = 0; j < intentLv; j++) {
            prefix = prefix + "\t";
        }
    }
    var line = "" + prefix + c + suffix;
    if (preChr == "\r\n" && line.substring(0, 1) == "\r\n") {
        line = line.substring(1);
    }
    editJson = editJson + "" + line;
    preChr = line.substring(line.length - 1, line.length);
}
// 改行が2つ続いている場合に、1つにまとめる
editJson = editJson.replace(/\r\n\r\n/g, "\r\n");
SelectAll(0);
InsText(editJson + "\r\n");

実行結果サンプル

整形前

{"employees":[{"firstName":"John","lastName":"Doe","age":35,"address":{"street":"123 Main St","city":"Anytown","state":"CA","zip":"12345"},"phoneNumbers":[{"type":"home","number":"555-555-1234"},{"type":"work","number":"555-555-5678"}]},{"firstName":"Jane","lastName":"Smith","age":25,"address":{"street":"456 Elm St","city":"Othertown","state":"NY","zip":"67890"},"phoneNumbers":[{"type":"home","number":"555-555-4321"},{"type":"work","number":"555-555-8765"}]}]}

マクロ実行後

{
	 "employees":[
		{
			 "firstName":"John"
			,"lastName":"Doe"
			,"age":35
			,"address":{
				 "street":"123 Main St"
				,"city":"Anytown"
				,"state":"CA"
				,"zip":"12345"
			}
			,"phoneNumbers":[
				{
					 "type":"home"
					,"number":"555-555-1234"
				}
				,{
					 "type":"work"
					,"number":"555-555-5678"
				}
			]
		}
		,{
			 "firstName":"Jane"
			,"lastName":"Smith"
			,"age":25
			,"address":{
				 "street":"456 Elm St"
				,"city":"Othertown"
				,"state":"NY"
				,"zip":"67890"
			}
			,"phoneNumbers":[
				{
					 "type":"home"
					,"number":"555-555-4321"
				}
				,{
					 "type":"work"
					,"number":"555-555-8765"
				}
			]
		}
	]
}
0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?