Skip to content

Instantly share code, notes, and snippets.

@wehrhaus
Last active June 6, 2018 04:21
Show Gist options
  • Save wehrhaus/5bd1b58928b501b42b32c27266be9562 to your computer and use it in GitHub Desktop.
Save wehrhaus/5bd1b58928b501b42b32c27266be9562 to your computer and use it in GitHub Desktop.
Perl Script for Transforming JSON
#!/usr/bin/env node
console.log(
`
FILENAME: transform-json.pl
#!/usr/bin/perl
#
# Takes a list of environment variables and applies
# - the values to the json files matching keys
# Expects the first argument to be path to json file
use JSON qw(encode_json decode_json);
my $json_file = $ARGV[0];
my $document = do {
local $/ = undef;
open my $fh, "<", $json_file
or die "Could not open $json_file: $!";
<$fh>;
};
my $json_obj = decode_json $document;
foreach $argnum (1 .. $#ARGV) {
$json_obj->{$ARGV[$argnum]} = $ENV{$ARGV[$argnum]};
}
open $document, ">", $json_file;
print $document encode_json $json_obj;
close $document;
`
);
{"name": "perl-to-json", "version": "0.0.0", "bin": "./index.js"}
#!/usr/bin/perl
#
# Takes a list of environment variables and applies
# - the values to the json files matching keys
# Expects the first argument to be path to json file
use JSON qw(encode_json decode_json);
my $json_file = $ARGV[0];
my $document = do {
local $/ = undef;
open my $fh, "<", $json_file
or die "Could not open $json_file: $!";
<$fh>;
};
my $json_obj = decode_json $document;
foreach $argnum (1 .. $#ARGV) {
$json_obj->{$ARGV[$argnum]} = $ENV{$ARGV[$argnum]};
}
open $document, ">", $json_file;
print $document encode_json $json_obj;
close $document;
@wehrhaus
Copy link
Author

wehrhaus commented Jun 6, 2018

index.js used to output transform-json.pl via npx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment