replacing a placeholder in a html file with 1-n specific values defined in a config.json using shellscript [closed]

we cannot use any 3rd party tools which are not usually present on systems

Given that we don’t know what “systems” you’re using, this isn’t a very useful restriction to tell us about.

Perl has several JSON parsers available and since Perl 5.14 (which was released in May 2011) one of them (JSON::PP) has been part of the standard Perl distribution. So if you have a version of Perl that was released in the last nine years, then the task of reading the JSON file into a variable is trivial.

#!/user/bin/perl

use strict;
use warnings;
use feature 'say';

use JSON::PP;
use Data::Dumper;

open my $json_fh, '<', 'config.json' or die $!;

my $json = do { local $/; <$json_fh> };

my $config = JSON::PP->new->decode($json);

# Now you have your JSON in a Perl data structure
say Dumper $config;

# You can also access individual values
say $config->{xxxx}{description};
say $config->{API}{value}{default}{host};

Browse More Popular Posts

Leave a Comment