You didn't provide the second JSON, so I had to hallucinate one myself. The comment shows where you need to adjust your code.

The basic idea is to index the first json by the external id using a hash.

#!/usr/bin/perl use warnings; use strict; use experimental qw( signatures ); use Cpanel::JSON::XS qw{ encode_json decode_json }; use List::Util qw{ shuffle }; sub create_json1($name, $size) { my @watchers; for my $i (shuffle(1 .. $size)) { push @watchers, { externalId => $i, watchers => [map "$_", map 1 + int rand $size, 0 .. ra +nd 4] }; } open my $out, '>', $name or die "$name: $!"; print {$out} encode_json(\@watchers); } sub create_json2($name, $size) { my @list; for my $i (shuffle(1 .. $size)) { push @list, { id => $i }; } open my $out, '>', $name or die "$name: $!"; print {$out} encode_json(\@list); } sub merge ($name1, $name2) { open my $in1, '<', $name1 or die "$name1: $!"; my $struct1 = decode_json(do { local $/; <$in1> }); open my $in2, '<', $name2 or die "$name2: $!"; my $struct2 = decode_json(do { local $/; <$in2> }); my %by_id = map { $_->{externalId}, $_->{watchers} } @$struct1; for my $member (@$struct2) { # This depends on the structure of the 2nd file. if (exists $by_id{ $member->{id} }) { $member->{watchers} = $by_id{ $member->{id} }; } else { warn "$member->{id} not found in $name1"; } } print encode_json($struct2); } my $SIZE = 100_000; create_json1('1.json', $SIZE); create_json2('2.json', $SIZE); merge('1.json', '2.json');

Runs under 2 seconds on my old machine.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: Combine 2 JSON files by choroba
in thread Combine 2 JSON files by gabecz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.