Created
October 24, 2016 19:59
-
-
Save theimpostor/79d4d37876aa990edd2ebc0e1d9391b5 to your computer and use it in GitHub Desktop.
Merge json files via simple perl script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
BEGIN { $ENV{PERL_JSON_BACKEND} = 'JSON::PP' } | |
use JSON; # always uses JSON:PP | |
use File::Slurp; | |
use Hash::Merge qw( merge ); | |
use warnings; | |
use strict; | |
## | |
# MAIN | |
## | |
MAIN: | |
{ | |
my $json = JSON->new->utf8->pretty->sort_by(sub { $JSON::PP::a cmp $JSON::PP::b }); | |
my $merged = {}; | |
foreach my $arg ( @ARGV ) { | |
my $json_text = read_file( $arg, binmode => ':utf8' ); | |
my $json_data = $json->decode( $json_text ); | |
$merged = merge( $merged, $json_data ); | |
} | |
print $json->encode( $merged ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment