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!In reply to Merging multiple JSON files into one using JSON::XS by walkingthecow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |