http://www.perlmonks.org?node_id=881859

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

Hi monks,
I am using JSON module for converting the Perl Hash into JSON string and JSON string into Perl hash. I want to check whether the string is in correct JSON format or not. I needed any function to test not any online JSON validator because I wanted to use that in a program. Please, help me if you know it.

Replies are listed 'Best First'.
Re: JSON Formatted String
by moritz (Cardinal) on Jan 12, 2011 at 10:00 UTC

      And if you want a really good instance from the Google search that moritz supplied, I highly recommend JSON lint.

Re: JSON Formatted String
by Your Mother (Archbishop) on Jan 12, 2011 at 15:02 UTC

    Use JSON::XS (which JSON will load if it's installed). Your JSON will be correct. I guarantee it, or your money back.

      I know that I can catch the error in particular function can be caught by using $@ variable. I will receive JSON formatted string from client socket. Then, I will pass that to from_json method to convert it into Perl Hash structure. Before I pass the string to that function, I want to verify whether the received string is in correct JSON format. If somebody knows function in JSON to check correct JSON string or not. Kindly help me out of this problem.

        I think you sort of answered your question there already. You just do it and catch the errors. No errors: valid, proceed. Errors... you decide what happens next. E.g.-

        use warnings; use strict; use JSON::XS; use Try::Tiny; my %json = ( empty_obj => "{}", empty_array => "[]", bare_string => "hai", obj => qq|{"o":"hai"}|, bad_obj => qq|{o:hai}|, ); for my $name ( keys %json ) { printf "%11s --> ", $name; try { my $throw_away = decode_json($json{$name}); print "OK!\n"; } catch { print "Error: $_"; }; } __END__ obj --> OK! bare_string --> Error: malformed JSON string, neither array, object, n +umber, string or atom, at character offset 0 (before "hai") at ... empty_obj --> OK! bad_obj --> Error: '"' expected, at character offset 1 (before "o: +hai}") at ... empty_array --> OK!

        Before I pass the string to that function, I want to verify whether the received string is in correct JSON format

        I know that I can catch the error in particular function can be caught by using $@ variable

        I'm confused. You've just answered your own question.

        my $valid = eval { $json->decode($input); 1 };
        Either you trust the JSON module to do its job, or you don't -- why wouldn't you trust the JSON module?
Re: JSON Formatted String
by sundialsvc4 (Abbot) on Jan 12, 2011 at 14:51 UTC

    What you do not want to do is to eval it, i.e. “just to see if it works.”   Many a website has been zapped by a maliciously-formed JSON string.

      Many a website has been zapped...

      Could you elaborate?  "Zapping" a web site would normally happen on the server, but how (or why) would you "eval" a JSON string server-side? As JSON is JavaScript, it can only be directly eval-ed by a JS interpreter. Sure you could run a JS interpreter server-side, but more typically, JS is used for client-side code, where eval-ing a malicious JSON string would unlikely have the effect of zapping the site... (at least it ought not be able to, as you can't really control what people do client-side anyway).