Last active
June 6, 2018 04:21
-
-
Save wehrhaus/5bd1b58928b501b42b32c27266be9562 to your computer and use it in GitHub Desktop.
Perl Script for Transforming JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; | |
` | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"name": "perl-to-json", "version": "0.0.0", "bin": "./index.js"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
index.js used to output
transform-json.pl
via npx