Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: JSON Formatted String

by Your Mother (Archbishop)
on Jan 13, 2011 at 05:54 UTC ( [id://882033]=note: print w/replies, xml ) Need Help??


in reply to Re^2: JSON Formatted String
in thread JSON Formatted String

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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (11)
As of 2024-03-28 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found