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

Hi,

We have developed a perl script to monitor the Google Applications stats but my plugin sometimes give an 'malformed JSON string,' when running the script.

[root@clrfmd1 libexec]# ./check_googlestats.pl --service='Google Calen +dar' malformed JSON string, neither array, object, number, string or atom, +at character offset 0 (before "(end of string)") at ./check_googlesta +ts.pl line 23

The plugin doesn't always throw this error and works pretty fine most of the time.

The URL that we are accessing is below

my $content = `curl http://www.google.com/appsstatus/json/en 2>/dev/nu +ll`; $content =~ s/dashboard.jsonp\(//g; $content =~ s/\)\;$//g;

The script is trying to process the response from JSON by decoding the URL.

Does anybody have any idea on this? There appears to somehow the parsing is wrong and I am unable to find the problem. Could somebody please let me know what changes are required to get this going.

Regards,
Sricharan

Replies are listed 'Best First'.
Re: JSON Parsing error in perl script
by davido (Cardinal) on Aug 26, 2014 at 04:28 UTC

    You should probably update your script such that when you get a "malformed JSON" error, the JSON that triggered the error would be dumped to a log file of some sort. By inspecting the malformed JSON, you will probably discover in what way it is mangled, and will have a better chance of figuring out how your code is mangling it.


    Dave

Re: JSON Parsing error in perl script
by NetWallah (Canon) on Aug 26, 2014 at 05:09 UTC
    Instead of throwing away the contents of STDERR, you should probably capture it using something like Open3 , and examine the contents if you run into an error.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

      Or better yet, use a Perl module like HTTP::Tiny to fetch the JSON instead of spawning an external process. This would allow you to gracefully handle non-200-OK HTTP responses.

Re: JSON Parsing error in perl script
by Anonymous Monk on Aug 26, 2014 at 02:32 UTC
    well, global substitutions don't make sense, other then that there is nothing
    s/^\Qdashboard.jsonp(\E//; s/\Q);\E$//;