Tuesday, February 10, 2015

format json with Gedit and Perl

How to prettify/format/indent json with Gedit and Perl:

install JSON::XS
go to Plugins and enable "External tool"

go to Tools -> Manage External Tools -> add new (the button in the bottom on the left if no icons are shown)
add a name, for example "Format JSON with Perl"
replace the contents of the text area on the right with

#!/usr/bin/env perl 

use strict;
use JSON::XS;
local $/ = undef;
my $content = <STDIN>;

my $j = JSON::XS->new->utf8->pretty(1);
my $output = $j->encode($j->decode($content));

print $output;

open your file, then Tools -> External Tools -> "Format JSON with Perl"

you can also set a shortcut in the Manage External Tools window

No comments:

Post a Comment