Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: parse html for specific things

by Laurent_R (Canon)
on Dec 15, 2017 at 22:34 UTC ( [id://1205628]=note: print w/replies, xml ) Need Help??


in reply to parse html for specific things

Yes, probably correct JSON, but badly formatted JSON, pretty much unreadable.

Replies are listed 'Best First'.
Re^2: parse html for specific things
by afoken (Chancellor) on Dec 16, 2017 at 10:53 UTC
    pretty much unreadable

    Probably because it is not intended to be read by humans. JSON parsers will have no problem with lack of irrelevant white space, so why bother adding white space? Compare with the HTML delivered by the Google start pages, or the "compressed" version of jQuery. Both are size-optimized input for the browser's parser, not intended to be read by humans in this form.

    My guess is that the JSON blob is loaded into a web page or into an appllication that parses the data and generates a nice, human readable view of the data.

    Alexander

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

      JSON is human readable. I think the comment relates to posting it on a site. Perhaps, for posting purposes, in the same we the guidelines suggest sensible layout of code/data example, it'd be wise for users to post pretty printed JSON to make it easier for humans here reading/responding to questions. Of course perl can help here, or there are simply online tools for doing so, for example http://jsonprettyprint.com.

      Probably because it is not intended to be read by humans. JSON parsers will have no problem with lack of irrelevant white space, so why bother adding white space?
      Yes, I fully appreciate that it doesn't matter for parsers. But, still, when I have to write some JSON or some HTML, I usually prefer to insert at least some vertical space (EOLs), because there is almost always a time somewhere in the future when I or another human will need to read the source or change it. My take on this is similar to what I do with Perl or C code: to make it friendly to the human reader. But I certainly understand your point that you may also need to optimize your document's size, especially if it's going to be transmitted zillions of times over a network which may sometimes be slow.

        Laurent_R:

        Yep, that's why my ~/bin directory contains json_prettify.pl:

        #!/usr/bin/perl # # json_prettify.pl <FName> # # Prettifies FName and writes it to FName.out # use strict; use warnings; use JSON; my $json = JSON->new->allow_nonref; while (my $FName = shift) { my $oname = $FName . ".out"; my $txt; { local $/; open my $FH, '<', $FName or die "no! $!\n"; $txt = <$FH>; } my $data = $json->decode($txt); $txt = $json->pretty->encode($data); open my $OFH, '>', $oname or die "No no no! $!\n"; print $OFH $txt; }

        I really need to make a thumb drive with my local ~/bin directory copied to it for when I visit other sites... ;^)

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re^2: parse html for specific things
by Dallaylaen (Chaplain) on Dec 19, 2017 at 08:18 UTC
    Readability of JSON is solved by a one-liner:
    perl -MJSON -we 'local $/; print JSON->new->pretty->canonical->encode( +decode_json(<>))'
    Too bad that doesn't work with anything Turing-complete, like JS code or Perl itself...

Log In?
Username:
Password:

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

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

    No recent polls found