Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Convert JS array of objects into Perl array of Hashes

by codeacrobat (Chaplain)
on Dec 08, 2009 at 21:38 UTC ( #811829=note: print w/replies, xml ) Need Help??


in reply to Convert JS array of objects into Perl array of Hashes

This is not exactly valid JSON for JSON::XS. Quotes are either missing or single quotes.

A bit of toying reveals:

echo "[{feedId:6161,feedType:'forum'},{feedId:1469,feedType:'category' +}]" | perl -MData::Dump=dd -MJSON::XS -ne 's{(\w+):}{"$1":}g; s{@{[ch +r 39]}}{"}g;dd decode_json($_)' [ { feedId => 6161, feedType => "forum" }, { feedId => 1469, feedType => "category" }, ]
Anyone know how to make JSON::XS more tolerant?

print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

Replies are listed 'Best First'.
Re^2: Convert JS array of objects into Perl array of Hashes
by afoken (Canon) on Dec 09, 2009 at 01:25 UTC
    Anyone know how to make JSON::XS more tolerant?

    JSON::XS has a "relaxed" mode (F_RELAXED in the XS code). Currently, it allows:

    • lists with a final comma (as in Perl)
    • Shell-style comments (from # to end of line) where JSON allowes white space

    This looks like a proper way to allow unquoted keys and the like, you "just" have to modify the XS code.

    The one thing that really drives me mad is the shell-style comment. Why on earth would someone want shell comments in JSON? JSON is derived from Javascript, and it is designed to be evaluateable by current browsers. Javascript has inherited its comment syntax from C and C++, i.e. // for single line comments and /* */ for comment blocks. I would like to see those comments supported by JSON::XS. No browser understands shell comments, so they should be removed.

    A different approach could be a subclass that uses the incremental parsing feature.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2021-09-17 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?