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:
[ { "name" : "bob", "title" : "janitor", "email" : "", "iq" : "180", "favorite_food" : "wagyu steak" }, { "name" : "joe", "title" : "software engineer", "email" : "", "iq" : "80", "favorite_food" : "raw hamburger" } ]
I am attempting to merge these files together so that they look like this:
{ "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" } ] }
The code I am using to do this is here:
#!/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;
Now, the problem I am facing is that the output looks like this:
{ "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

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.