Hi,
I'm trying to iterate through a json decoded string (using JSON::PP's decode) in order to replace individual elements with corresponding perl objects (e.g. strings starting with ISODate with Time::Moment). i keep running into issues due to the structure of the json_decoded object. i'm new to per and traversing the structure to get to the parts i want to replace is a nightmare of hash/array referencing/dereferencing. here's what i have. could someone please point me to where i'm going wrong?
my $qstr = 'MONGOQUERY[
{
$match: {
$or : [{"prefix" : "27450500"}, {"creationDate": ISODa
+te("2017-06-18T12:52:16.000")}]
}
},
{
$project: {
_id:0,
prefix:1
}
},
]';
$qstr=~s/MONGOQUERY//gs;
$qstr=~s/ISODate\("([^)]*?)\)/"ISODate$1/gs;
#Timestamp,ISODate, NumberDecimal,NumberLong,NumberInt,Symbol
my $qarrayref = JSON::PP->new->allow_nonref->relaxed->allow_barekey->l
+oose->decode($qstr);
my $rlimit=50;
repl_mongo(\@{$qarrayref},$rlimit);
sub repl_mongo{
if($_[1]>0){
open(my $fh, '>>', '/tmp/mongosample.txt');
if(ref($_[0]) eq 'ARRAY'){
foreach my $item(@{$_[0]}){
repl_mongo($item,$_[1]-1 );
}
}elsif(ref($_[0]) eq 'HASH'){
foreach my $item (keys %{$_[0]}){
repl_mongo($key,$_[1]-1 );
}
}else{
if(index("ISODate", $_[0]) >= 0){
$_[0] =~s/ISODate//gs;
# $_[0] =Time::Moment->from_string($_[0]);
}
}
close $fh;
}
}
open(my $fh, '>>', '/tmp/mongosample.txt');
print $fh Dumper($qarrayref);
close $fh;