Reputation: 128
I am trying to strip double quotes around numbers in all arrays from a JSON output. e.g.
["123","345","5567" ...]
should become:
[123, 345, 5567 ...]
I can locate the array part using something like:
/\[("(?<name>\d+)"[,]*)*\]/
However, I didn't succeed to replace (?<name>\d+)
with $+{name}
because there are multiple occurrences of $+{name}
... Can anybody shed me any light?
Upvotes: 0
Views: 95
Reputation: 67900
You can read the data into a Perl data structure with the JSON
module, convert the strings to numbers, then output the json again:
use strict;
use warnings;
use Data::Dumper;
use JSON;
my $string = qq(["123","345","5567"]);
my $data = from_json($string);
print Dumper to_json($data); # datastructure before conversion
$_ += 0 for @$data;
print Dumper to_json($data); # after
Output:
$VAR1 = '["123","345","5567"]';
$VAR1 = '[123,345,5567]';
Of course, this will only work for strings that are numbers, so you will need to validate your data if you have non-numbers in there.
Upvotes: 5