Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Not an ARRAY reference

by roboticus (Chancellor)
on Nov 08, 2020 at 15:09 UTC ( #11123503=note: print w/replies, xml ) Need Help??


in reply to Re^2: Not an ARRAY reference
in thread Not an ARRAY reference

chandantul:

I am getting an error after few times running the code
"JSON error at line 1, byte 1/156: Unexpected character 'C' parsing in +itial state: expecting whitespace: 'n', '\r', '\t', ' ' or start of s +tring: '"' or digit: '0-9' or minus: '-' or start of an array or obje +ct: '{', '[' or start of literal: 't', 'f', 'n' at All-users-Apps.pl +line 158."
Please let me know is this is a code issue or network issue.

With an error like this, it's not likely to be either a code or network issue. The error message is telling you that it's having trouble parsing the JSON data, so it's almost certainly a data issue. Since it tells you where in the data the error is, you should look at the JSON data to see what it looks like, and see if there's an obvious data problem.

Normally when I see a JSON error, it comes down to one of the following:

  • There was an error on the server and instead of it sending JSON, it sent an error message and my code failed to check for errors before parsing the JSON.
  • Someone created the JSON by just pasting strings together rather than a JSON library, and they created some bogus JSON with an error like:
    • A text string contained a newline, so the JSON record was unterminated.
    • A text string contained a quote breaking the JSON quoting rules.
    • The sender got some trace/debug messages mixed up in the JSON string.
    • . . . other cases I don't recall off hand . . .
  • There's a mismatch in the encoding/decoding (for unicode, HTML entities, or other) that's butchered the JSON. This is one of the cases where it's more likely to be a code issue than a data issue, as the code steps on the data.
  • Congratulations! you've found a bug in a JSON routine... Note: Very unlikely if you're using a decent JSON package, as it would be used by many thousands of people in many applications. I won't say impossible as I've found a couple compiler bugs in several decades of programming, but I'd take a hard look at the incoming JSON code, my own code that processed the data long before I'd start suspecting a bug in a well-used / well-tested JSON library. I've never actually encountered this situation, I just included it for completeness.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

2020-11-09 Athanasius fixed over-long line.

Replies are listed 'Best First'.
Re^4: Not an ARRAY reference
by chandantul (Sexton) on Nov 08, 2020 at 16:52 UTC

    Thanks for your responses. I have also observed that JSON parsing is fine till 4000 but its complaining against 4001 lines but when i ran my code 2nd time its got stuck at 8000 lines and so on. How i can ignore the problem line and go to next line. and Continue

      chandantul:

      If you've got one JSON entry per line and are using the JSON package from cpan.org, then you'd handle the exception something like this:

      use warnings; use strict; use JSON; . . . # Build a JSON parser my $json = JSON->new->allow_nonref; # Process the lines in the file, we're labelling the WHILE loop to mak +e it obvious # where we skip to after handling the exception JSON_RECORD: while (my $line = <INFILE>) { # We'll declare the variable so we can use it outside of the eval my $data; # Parse the data inside the eval so we can catch the error eval { $data = $json->decode($line); }; # Now we can check $@ to see if we had an exception if ($@) { # parse failed, so log it and let the user know print "JSON parse failed on line $.\n"; print "error was: $@\n"; print "JSON we tried to parse <<$line>>\n\n"; # Now that we've handled the exception, skip to the next recor next JSON_RECORD; } # OK if we get here, we've successfully parse the JSON data, so we + can do what # we normally do now. . . . }

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Thanks for your response, Please check my Json data one more time. This is an example. There will be 30000 data like this and i am suspecting one of the records creating an issue which JSON response had some issue, The users records under {}, under [] Its not line specific but a record under {}, under []. Will your above code works for each users JSON response? The JSON responses hold by $responsetextall[$i]

        [ { 'status' => 'ACTIVE', 'activated' => '2020-04-29T17:58:43.000Z', 'created' => '2020-04-29T17:58:42.000Z', '_links' => { 'self' => { 'href' => 'https://ssoqa.abcoffi +ce.com/api/v1/users/abcdefghijklmno55' } }, 'passwordChanged' => undef, 'lastUpdated' => '2020-11-06T19:09:08.000Z', 'id' => '00abcdefghijklmn6', 'lastLogin' => '2020-10-05T15:16:53.000Z', 'profile' => { 'email' => 'soula.olla@abc.com', 'middleName' => 'Y', 'countryCode' => 'IN', 'login' => '123456789@ADSYF.SYFBANK.COM', 'streetAddress' => '14-45,idhaian', 'organization' => 'ABC Organization', 'employeeNumber' => '123456789', 'division' => 'ABC - IT', 'AWSrole' => [], }, 'type' => { 'id' => 'omstdyc' }, 'credentials' => { 'provider' => { 'type' => 'AD', 'name' => 'AD.COM' } }, 'statusChanged' => '2020-06-01T08:56:35.000Z' }, { 'id' => '00mnopqrstq6', 'lastLogin' => ${\$VAR1->[0]{'passwordChanged'}}, 'profile' => { 'managerId' => '2456789', 'userType' => 'Contract', 'city' => 'Stamford', 'zipCode' => '00009', 'JobFunction' => 'IT', 'manager' => 'Kal, R', 'department' => 'IT - IN - CS&T - Client Ec +om', 'displayName' => 'Bh, M', 'lastName' => 'Bht', 'streetAddress' => '7 Lo Ri Rd', 'email' => 'R.Kal@syf.com', }, 'type' => { 'id' => 'obcdefghijklmn5' }, 'credentials' => { 'provider' => { 'type' => 'AD', 'name' => 'AD.ABC.COM' } }, 'statusChanged' => '2020-04-29T17:58:49.000Z', 'status' => 'ACTIVE', 'activated' => '2020-04-29T17:58:49.000Z', 'created' => '2020-04-29T17:58:47.000Z', '_links' => { 'self' => { 'href' => 'https://ssoqa.abc.com +/api/v1/users/00abcdefghijkl6' } }, 'passwordChanged' => ${\$VAR1->[0]{'passwordChanged'}}, 'lastUpdated' => '2020-11-06T19:09:10.000Z' }, ]
        for my $i (0..$#responsetextall) { $responsetextall[$i] =~ s/]\[/,/g; for my $j (0..$#{$responsetextall[$i]}) { my $responseid = $responsetextall[$i][$j]{id}; my $responsests = $responsetextall[$i][$j]{status}; if ($responsests ne "Deactivated") { + run_api_call($apiurluser. $responseid . $apiurl2); my @responsetext4 = parse_json ($client->responseContent()); my $responsecode1 = $client->responseCode() ; for my $m (0..$#responsetext4) { for my $n (0..$#{$responsetext4[$m]}) { + $response1 = $responsetext4[$m][$n]{id}; $resplable = $responsetext4 [$m][$n]{l +abel}; push @mouapps, $resplable; } } my $newurl = ($apiurluser . $responseid ); + run_api_call($newurl); + my @responsetext1 = parse_json ($c +lient->responseContent()); print Dumper @responsetext1 ; for my $l (0..$#responsetext1) { my $response2 = $responsetext1[$l]{id}; my $response3 = $responsetext1[$l]{profile}{firstName}; my $response4 = $responsetext1[$l]{profile}{lastName}; my $response5 = $responsetext1[$l]{profile}{email}; my $response6 = $responsetext1[$l]{credentials}{provide +r}{name}; ## Wrting to the reporting file. $worksheet->write(0, 0, 'OKTA-ID'); $worksheet->write(0, 1, 'FIRST NAME'); $worksheet->write(0, 2, 'LAST NAME'); $worksheet->write(0, 3, 'EMAIL-ID'); $worksheet->write(0, 4, 'Profile-Master'); $worksheet->write(0, 5, 'APPS'); $worksheet->write($r, 0, $response2); $worksheet->write($r, 1, $response3); $worksheet->write($r, 2, $response4); $worksheet->write($r, 3, $response5); $worksheet->write($r, 4, $response6); foreach (@mouapps) { local $" = '|'; my $response7 = join ("|","@mouapps"); $worksheet->write($r, 5, $response7); } $r += 1; } } else { print "Do Nothing" ; } } } $workbook->close; print "Spreadsheet saved.\n";

        2020-11-09 Athanasius added code tags around JSON data.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11123503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2021-01-27 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?