Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

json text or perl structure exceeds maximum nesting level

by anjultyagi (Novice)
on Jun 01, 2018 at 07:37 UTC ( [id://1215597]=perlquestion: print w/replies, xml ) Need Help??

anjultyagi has asked for the wisdom of the Perl Monks concerning the following question:

I am working on to migrate the PHP code to PERL, I am facing issue while encoding the object into JSON. it gives me an error : json text or perl structure exceeds maximum nesting level (max_depth set too low?).
I have tried the JSON:PP and JSON::XS but did not get success. I tried to setup the max_depth value, after that process is keep running and running.

below are the code itried:
my $json = JSON::XS->new->utf8->max_depth([100]); #$json->encode($data) my $json_object = $json->encode($output);

Replies are listed 'Best First'.
Re: json text or perl structure exceeds maximum nesting level
by Corion (Patriarch) on Jun 01, 2018 at 07:43 UTC

    First, your call to ->max_depth doesn't do what you think it does. I think you should call it as:

    JSON::XS->new->utf8->max_depth(100);

    that is, leave the square brackets out.

    Second, maybe your data structure cannot be serialized as JSON because it is circular? Have you inspected your data structure using Data::Dumper to see what it looks like?

    #!perl -w use strict; use JSON::XS; # Create a simple circular data structure: my $output; $output = ['foo']; $output->[1] = $output; my $json = JSON::XS->new->utf8->max_depth(100); use Data::Dumper; print Dumper $output; print $json->encode($output); __END__ json text or perl structure exceeds maximum nesting level (max_depth s +et too low?) at tmp.pl line 13.
      If I was a gambler, I would say that a circular structure is almost certain to be the problem here.
Re: json text or perl structure exceeds maximum nesting level
by choroba (Cardinal) on Jun 01, 2018 at 07:42 UTC
    The default max_depth for both JSON::PP and JSON::XS is 512 (and the same for Cpanel::JSON::XS), far beyond 100 from your example.

    How deep is your actual structure?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: json text or perl structure exceeds maximum nesting level
by anjultyagi (Novice) on Jun 05, 2018 at 13:22 UTC
    Thanks guys for your help.. I have verified my object using data::dumper after that it works fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1215597]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found