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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.