Skip to content

Instantly share code, notes, and snippets.

@theimpostor
Created October 24, 2016 19:59
Show Gist options
  • Save theimpostor/79d4d37876aa990edd2ebc0e1d9391b5 to your computer and use it in GitHub Desktop.
Save theimpostor/79d4d37876aa990edd2ebc0e1d9391b5 to your computer and use it in GitHub Desktop.
Merge json files via simple perl script
#!/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