#!perl use 5.020; use JSON::Tiny 'decode_json', 'encode_json'; sub read_json { my ($filename) = @_; open my $fh, '<', $filename or die "Couldn't read '$filename': $!"; return decode_json( do { local $/; <$fh> } ); # sluurp } my $first_json_file = 'first.json'; my $second_json_file = 'second.json'; my $data_first = read_json( $first_json_file ); my $data_second = read_json( $second_json_file ); my %watchers; for my $w ($data_first->@* ) { $watchers{ $w->{ externalId } } = $w->{ watchers }; } for my $o ($data_second->@* ) { my $id = $o->{externalId}; if( ! $watchers{ $id } ) { warn "Second item '$id' does not have watchers?!"; } else { $o->{ watchers } = $watchers{ $id }; } }; say json_encode( $data_second );