in reply to Merging multiple JSON files into one using JSON::XS

This looks like a weird hack, but it works to produce the desired output:
my $pretty_printed_unencoded = $coder->encode ({People => @fields });
I have no idea how/why - it makes no sense to me. Would love to be enlightened on what the "right" way is, and why this works.

Update:: After seeing kcott's(++) post below, I see the error of my ways.

His solution is more elegant, but I made mine work by coding:

my $pretty_printed_unencoded = $coder->encode ({People => [map {@$_} +@fields] });
The previous version at top of this post worked for me for ONE file.

             My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Replies are listed 'Best First'.
Re^2: Merging multiple JSON files into one using JSON::XS
by walkingthecow (Friar) on Aug 12, 2013 at 13:01 UTC
    This did not seem to work for me for some reason, I'd either get this error:
    Odd number of elements in anonymous hash
    or I'd end up with this:
    { "ARRAY(0x834963c)" : [ {
    That "ARRAY..." is supposed to be People. Here's the code I used.
    #!/usr/local/perl5/bin/perl use strict; use warnings; use JSON::XS; use File::Slurp qw( read_file ); use Data::Dumper; open(my $fh, '<', '/tmp/test') or die $!; # contains list of file name +s my @fields; while(<$fh>) { chomp; next if !-e "/tmp/files/$_.json"; my $decoded = decode_json( read_file("/tmp/files/$_.json") ); push @fields, $decoded; } close $fh; my $coder = JSON::XS->new->ascii->pretty->allow_nonref; my $encoded = $coder->encode ({People => @fields }); print $encoded;

    If I added a \ before fields (i.e., \@fields) then I'd get People as expected, but I'd also get the same output I was dealing with before (each file being its own array). Also, the slash in front of fields made it so I would no longer get the error about having an odd number of elements.

    Though it didn't quite work I really appreciate you taking the time to try and figure it out with me :)