Q:

Perl encode_json function is not formatting correctly

I have a few files full of IP addresses, one address per line.  I want to combine them all together into one JSON file in this format:
["1.1.1.1","2.2.2.2","3.3.3.3"]

I then want to write this to a new file.  Please see my code and see my outputs at two different stages.  I think it is a simple problem to fix but I am not seeing it. 
#! /usr/bin/perl
#install JSON module 'sudo cpanm JSON'
use warnings;
use strict;
use JSON;

my %data;
my @FILES = glob("/home/jamie/store/inbound/threatfeeds/*_ip");

# 1.  Open and load each file    
foreach my $file (@FILES) {
    local $/ = undef;
    open my $fh, '<', $file;
    $data{$file} = <$fh>;
}

foreach my $file (@FILES) {
    $data{$file} = qx(/bin/cat "$file");
}

use File::Slurp;
foreach my $file (@FILES) {
    $data{$file} = File::Slurp::slurp($file);
}

#convert hash array to JSON
#print %data;
my $json_output = encode_json \%data;
print $json_output;

The output as the code stands currently is something similar to this: 
216.243.31.254\n195.239.244.122\n103.10.133.179\n198.13.96.39\n198.13.96.59\n198.13.96.233\n104.167.119.161\n193.201.227.90\n208.67.1.21\n175.139.186.213

If I uncomment print %data and comment out print $json_output, I get a list of IP addresses similar to this:
109.161.206.153
91.205.173.220
66.196.243.4

A:

I suspect what you want is pretty_print:
print to_json ( \%data, { 'pretty_print' => 1 } );

But can I also point out - it's thoroughly nasty to system cat.  Perl has perfectly good open system calls.  
Especially as you basically do it twice and clobber your data in the process.  
Why are you trying to embed all of a file into a json structure as plain text?  
Do you mean that you are perhaps trying to make a JSON array with one IP per element?  Because what you're doing... isn't going to work.  
How about:
#! /usr/bin/perl
#install JSON module 'sudo cpanm JSON'
use warnings;
use strict;
use JSON;
use autodie;

my @data;

foreach my $file ( glob("/home/jamie/store/inbound/threatfeeds/*_ip") ) {
    open( my $input, "<", $file );
    while (<$input>) {
        chomp;
        my ($ip_addr) = m/([\d\.]+)/;
        push( @data, $ip_addr );
    }
    close($input);
}

my $json_output = to_json( \@data, { pretty => 1 } );
print $json_output;

Assuming your files are just a list of IP addresses, this will give you an 'ip' json array 'looking like': 
[
    "10.1.2.3",
    "192.168.0.22",
]

