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

Hi all - I looking for advise regarding the following error

 malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "<br />\n<b>Warning</...") at /usr/share/perl5/JSON.pm line 171

Hope you dont mind asking - I get this error when uploading to something called PIWIGO. An online image gallery. The perl script itself is available here http://piwigo.org/ext/extension_view.php?eid=606

While images do upload fine I get the above error and then the uploading stops. If I run the script again it can be seen that the image it seemed to fail on did upload and then it moves onto the nexxt image and then again the error pops up again.

If someone has any ideas that would be great....thanks for any advice

Replies are listed 'Best First'.
Re: Malformed JSON Error Piwigo
by NetWallah (Canon) on Apr 26, 2017 at 13:58 UTC
    The code has no error/warning check, for returned content.

    I think the problem area is:

    my $response = $ua->post( $conf{base_url}.'/ws.php?format=json', { method => 'pwg.categories.getImages', cat_id => $params{album_id}, per_page => 1000000, } ); foreach my $image_href (@{from_json($response->content)->{result}{ +images}}) { $photos_of_album{ $params{album_id} }{ $image_href->{file} } = + $image_href->{id}; }
    It looks like the web server is trying to return a WARNING in the returned content.
    The programmer should check $response->content for Errors/warnings prior to calling "from_json".

    I would probably place the blame on the server programmer, for not returning the error/warning in JSON format, as requested.

            ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

      Just to be clear do you mean the script is dodgy itself or the response from the hosting provider which is Awardspace

      Apologies all noob queries

        Both sides.

        The script you are using to fetch data from the server expects JSON back, and croaks if it does not get it.
        That part could be improved with Error/Warning detection and handling.

        The server side code that runs on the hosting service is what provides a response to a request from your clinet script.
        That should also behave itself when JSON is requested, and provide a JSON-only response.
        Apparently, it is not doing so in this case - it is sending an HTML style warning message.
        You can blame (lack of) documentation for this, and/or poor design/coding/testing.

                ...Disinformation is not as good as datinformation.               Don't document the program; program the document.