walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:
Evening monks. Sorry to be asking such a simple question, but it seems that this one has my brain in a knot. I have a directory with a bunch of JSON files that look like this:
I am attempting to merge these files together so that they look like this:[ { "name" : "bob", "title" : "janitor", "email" : "", "iq" : "180", "favorite_food" : "wagyu steak" }, { "name" : "joe", "title" : "software engineer", "email" : "", "iq" : "80", "favorite_food" : "raw hamburger" } ]
The code I am using to do this is here:{ "People" : [ { "name" : "bob", "title" : "janitor", "email" : "", "iq" : "180", "favorite_food" : "wagyu steak" }, { "name" : "joe", "title" : "software engineer", "email" : "", "iq" : "80", "favorite_food" : "raw hamburger" }, { "name" : "sandy", "title" : "dishwasher", "email" : "", "iq" : "240", "favorite_food" : "filet mignon" }, { "name" : "george", "title" : "software engineer", "email" : "", "iq" : "14", "favorite_food" : "tacos" } ] }
Now, the problem I am facing is that the output looks like this:#!/usr/local/perl5/bin/perl use strict; use warnings; use JSON::XS; use File::Slurp qw( read_file ); open(my $fh, '<', '/tmp/test') or die $!; # contains list of file name +s my @fields; my $uuts = {}; while(<$fh>) { chomp; next if !-e "/tmp/files/$_.json"; my $decoded = decode_json( read_file("/tmp/files/$_.json") ); push @fields, $decoded; } $uuts->{People} = [ @fields ]; my $coder = JSON::XS->new->ascii->pretty->allow_nonref; my $pretty_printed_unencoded = $coder->encode ($uuts); print $pretty_printed_unencoded;
{ "People" : [ [ { "name" : "bob", "title" : "janitor", "email" : "", "iq" : "180", "favorite_food" : "wagyu steak" }, { "name" : "joe", "title" : "software engineer", "email" : "", "iq" : "80", "favorite_food" : "raw hamburger" } ], [ { "name" : "sandy", "title" : "dishwasher", "email" : "", "iq" : "240", "favorite_food" : "filet mignon" }, { "name" : "george", "title" : "software engineer", "email" : "", "iq" : "14", "favorite_food" : "tacos" } ] ] }
I don't want the content of each file to be its own array. I want People to be an array whose contents is the content of each file. Yet, for the life of me I cannot seem to accomplish this.
Any help is greatly appreciated. Thank you Monks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Merging multiple JSON files into one using JSON::XS
by kcott (Archbishop) on Aug 12, 2013 at 08:59 UTC | |
by walkingthecow (Friar) on Aug 12, 2013 at 12:56 UTC | |
Re: Merging multiple JSON files into one using JSON::XS
by NetWallah (Canon) on Aug 12, 2013 at 03:54 UTC | |
by walkingthecow (Friar) on Aug 12, 2013 at 13:01 UTC | |
Re: Merging multiple JSON files into one using JSON::XS
by frozenwithjoy (Priest) on Aug 12, 2013 at 02:47 UTC | |
by walkingthecow (Friar) on Aug 12, 2013 at 02:49 UTC | |
Re: Merging multiple JSON files into one using JSON::XS (array or hash or object)
by Anonymous Monk on Aug 12, 2013 at 07:02 UTC |
Back to
Seekers of Perl Wisdom