Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Merging multiple JSON files into one using JSON::XS

by walkingthecow (Friar)
on Aug 12, 2013 at 13:01 UTC ( [id://1049117]=note: print w/replies, xml ) Need Help??


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

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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1049117]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found