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

Re^3: JSON Formatted String

by Your Mother (Archbishop)
on Jan 13, 2011 at 05:54 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2021-01-22 19:22 GMT
Find Nodes?
    Voting Booth?