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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.