Format JSON with jEdit

Sometimes I have to debug or check a json response from the server: I just paste the response to jEdit and reformat it with a macro (yes, I know about the online json beautifier and I think we did have a firebug extension for this, but still, it’s fun to see how flexible javascript macros are):

Download the json2.js from json.org, place it into your lib directory (see: startup.js), then use this ridiculously simple macro to reformat a valid javascript object (in buffer or selection):

(function(){
if (buffer.isReadOnly())
return;

var text = "",
selections = textArea.getSelection(),
bufferSelected = false,
obj;

if (selections.length == 1) {
text = textArea.getSelectedText(selections[0]);
} else {
text = textArea.getText();
bufferSelected = true;
}

obj = eval("(" + text + ")");
include(MACROLIBPATH + "json.org/json2.js");
textArea[bufferSelected ? "setText" : "setSelectedText"](JSON.stringify(obj, null, '\t'));
})();

You can download this and some of my other macros from here.

Tags: ,

Comments are closed.