Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: JSON decode problem

by kennethk (Abbot)
on Aug 05, 2016 at 15:14 UTC ( [id://1169189]=note: print w/replies, xml ) Need Help??


in reply to Re: JSON decode problem
in thread JSON decode problem

Please read my node. showParams cannot be evalled because of syntax errors. If a data stream is emitted with syntax errors of this magnitude, the content had better be valuable the fixes you make are going to be incredibly fragile.

For example, in order to clean showParams, you need to detect unescaped quotes and escape them. For the sample you could run

my $str = 'showParams: "{"revertType":null,"refresh":false,"checkpoint +":false,"sheetName":"","unknownParams":"zone_id=6290&ac=a9aef098062f9 +646bd1638d0c6054c7b&account_id=62&Start_Date=2016-07-27&End_Date=2016 +-08-03","layoutId":""}"'; while ($str =~ /(?<!\\)"\s*\{\s*("(?:(?!\}").)*)\}"/) { substr($str, $-[1], $+[1] - $-[1]) =~ s/(?=["\\])/\\/g; }
but good luck maintaining that.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^3: JSON decode problem
by FreeBeerReekingMonk (Deacon) on Aug 11, 2016 at 21:16 UTC
    Well, that is the thing everyone is pointing out. You can either write a pre-parser that fixes it into standard JSON, then read it with the JSON modules, or parsing it yourself (which could mean you will have to implement much more).

    From JSON#Data_portability_issues

    Despite the widespread belief that JSON is a strict subset of JavaScript, this is not the case. Specifically, JSON allows the Unicode line terminators U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR to appear unescaped in quoted strings, while JavaScript does not. .

    In "Unsupported native data types" Wikipedia specifically states for Date() that there are some de facto standards, e.g., converting from Date to String, but none universally recognized. This JSON is "weird" like that.

    So each line is an identifier: thing where thing can be:

    (something that must be evaluated in Javascript itself)

    "{"...."}" a hash

    "a string"

    false a boolean

    new Date(... a date

    Strings and booleans are json already, the others, not that much...

      As fellow monks have said, the problem here is that it is not portable json, it needs to be evalled into a javascript environment, like your browser.
      This was the specific point I was replying to. It is not valid JavaScript due to unescaped quotes, and therefore you can't leverage a JavaScript interpreter.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found