Assuming that you can fix your JSON (a trailing comma is not allowed) I'd recommend to process this using a module, and in a script.
An example with JSON, wrapped in a "one"-liner as asked
perl -MPath::Tiny -MJSON -0777 -wnE'
my $hr = decode_json $_;
unshift @{$hr->{archive}}, $hr->{main};
$hr->{main} =~ s/[0-9]+\.[0-9]+.\K([0-9]+)/$1+1/e;
path("new_".$ARGV)->spew(encode_json $hr)'
' data.json
The very handy Path::Tiny is used to easily dump JSON output. I make the output file name by prefixing new_
to the input filename (available in $ARGV variable), adjust as suitable.
If installing a module is a problem for some reason you can instead simply print the JSON encoded string and redirect the output
perl -MJSON -0777 -wnE'
my $hr = decode_json $_;
unshift @{$hr->{archive}}, $hr->{main};
$hr->{main} =~ s/[0-9]+\.[0-9]+.\K([0-9]+)/$1+1/e;
say encode_json $hr
' data.json > new_data.json
These produce the output file with   {"archive":["3.3.0","3.2.2"],"main":"3.3.1"}
With the -0777
command switch the whole file is "slurped" into a scalar ($_
) and -M...
loads the given module. Then we use decode_json
, which JSON
exports by default in its functional interface, to obtain a hashref with data.
The current value of main
is then added to the beginning of the arrayref in archive
using unshift, and is then changed by bumping up its release/patch number, using a regex.
Finally the encode_json
, also exported in JSON
's functional interface, is used to JSON-encode the hashref, what is dumped either with Path::Tiny
to a file or to STDOUT for redirection.
A word on the number of existing JSON modules is in order, prompted by comment by Grinnz.
The JSON linked above will load the JSON::XS module, and if that is not installed then it falls back to the compatible pure-Perl one, JSON::PP. I'd recommend the XS module, which is much faster and proven in wide use.
Another option is Cpanel::JSON::XS, the JSON::XS
fork with a bug tracker and a list of bug fixes. I've happily used both without any issues.
To have these tried in order Cpanel::
then ::XS
then ::PP
, also with some improvements over JSON
in how backends are loaded, use JSON::MaybeXS.
Note that none of these are in core.