Loading ...
Sorry, an error occurred while loading the content.

Re: [ydn-javascript] Re: Any JSON Pretty-Printers Out There?

Expand Messages
  • Satyam
    I pasted a JSON string into a new file declared as JavaScript in Eclipse and asked it to format it as JavaScript. I had to put a var a = in front of it and a
    Message 1 of 10 , Dec 1, 2007
    • 0 Attachment
      I pasted a JSON string into a new file declared as JavaScript in Eclipse and
      asked it to format it as JavaScript.

      I had to put a

      var a =

      in front of it and a semicolon at the end, the formatter didn't want to
      start while there were warnings, but that is something that will depend on
      the IDE. But basically the idea is that JSON is JavaScript and any
      formatter for JavaScript should be able to handle it.

      Satyam


      ----- Original Message -----
      From: "Randall R Schulz" <rschulz@...>
      To: <ydn-javascript@yahoogroups.com>
      Sent: Saturday, December 01, 2007 11:05 PM
      Subject: Re: [ydn-javascript] Re: Any JSON Pretty-Printers Out There?


      > On Saturday 01 December 2007 13:42, Satyam wrote:
      >> I just noticed that if in the Watch tab on the Firebug debugger (for
      >> Firefox) you type the expression:
      >>
      >> eval('(' + myJsonString + ')')
      >>
      >> it will show it just like any other object like a tree which you can
      >> expand/collapse at will.
      >
      > That's nice, but the use case for me is viewing the responses from my
      > server code. I generally use "wget" or "dog" to retrieve them and would
      > like to view those responses in a text viewer ("less") / editor ("vim")
      > in my shell environment.
      >
      >
      >> Satyam
      >
      >
      > RRS
      >
      >
      >
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      > --
      > No virus found in this incoming message.
      > Checked by AVG Free Edition.
      > Version: 7.5.503 / Virus Database: 269.16.12/1162 - Release Date:
      > 30/11/2007 21:26
      >
      >
    • Ask Bjørn Hansen
      ... As someone else said, a few words of Perl: perl -MJSON -e $j=JSON- new(pretty= 1); $/=undef; print $j- ... You can either pipe in ugly JSON on stdin or
      Message 2 of 10 , Dec 2, 2007
      • 0 Attachment
        On Nov 30, 2007, at 10:50 AM, Randall R Schulz wrote:

        > Compact JSON, as most web apps produce it, is very hard to read.
        >
        > Does anyone here know of a reformatter / pretty-printer for JSON? I'd
        > prefer a command-line tool that runs on (or can be compiled for)
        > Linux.
        > If necessary, I'd use an on-line tool.


        As someone else said, a few words of Perl:

        perl -MJSON -e '$j=JSON->new(pretty=>1); $/=undef; print $j-
        >objToJson($j->jsonToObj(<>))'

        You can either pipe in "ugly" JSON on stdin or specify a file as a
        parameter at the end. Stdout will have the pretty JSON.


        - ask

        --
        http://develooper.com/ - http://askask.com/
      • Randall R Schulz
        ... Perl s not my thing. I took your command and used the URL that freyfogle gave to get JSON.pm (and JSON/Parser.pm, which it refers to and without which it
        Message 3 of 10 , Dec 2, 2007
        • 0 Attachment
          On Sunday 02 December 2007 18:39, Ask Bjørn Hansen wrote:
          > On Nov 30, 2007, at 10:50 AM, Randall R Schulz wrote:
          > > Compact JSON, as most web apps produce it, is very hard to read.
          > >
          > > Does anyone here know of a reformatter / pretty-printer for JSON?
          > > I'd prefer a command-line tool that runs on (or can be compiled
          > > for) Linux.
          > > If necessary, I'd use an on-line tool.
          >
          > As someone else said, a few words of Perl:
          >
          > perl -MJSON -e '$j=JSON->new(pretty=>1); $/=undef; print $j-
          >
          > >objToJson($j->jsonToObj(<>))'
          >
          > You can either pipe in "ugly" JSON on stdin or specify a file as a
          > parameter at the end. Stdout will have the pretty JSON.

          Perl's not my thing.

          I took your command and used the URL that freyfogle gave to get JSON.pm
          (and JSON/Parser.pm, which it refers to and without which it will not
          run) and I when I invoke it (the command above) I get a pile (246 lines)
          of diagnostics. Here are the first few:

          Bareword found where operator expected at JSON/Parser.pm line 4, near ""stylesheet" href"
          (Missing operator before href?)
          Bareword found where operator expected at JSON/Parser.pm line 4, near ""/s/style.css" type"
          (Missing operator before type?)
          Bareword found where operator expected at JSON/Parser.pm line 7, near "</head"
          (Might be a runaway multi-line // string starting on line 6)
          (Missing operator before head?)


          What's the right way to use this stuff? I'm running on Linux (SuSE Linux 10.0)


          > - ask


          Thanks.

          Randall Schulz
        • freyfogle
          this can be greatly simplified, but for clarity I ve been fairly verbose: #!/usr/bin/perl -w use strict; use JSON; my $file = somefile.json ; my $JSON = new
          Message 4 of 10 , Dec 3, 2007
          • 0 Attachment
            this can be greatly simplified, but for clarity I've been fairly verbose:

            #!/usr/bin/perl -w

            use strict;
            use JSON;
            my $file = "somefile.json";
            my $JSON = new JSON;
            my $js;

            # read the file into a variable
            open(F,$file) or die "cant open $file $!";
            while (my $line = <F>){
            $js .= $line;
            }
            close (F);

            # turn JSON into an object
            my $obj = $JSON->jsonToObj($js);

            # pretty-printing
            print $JSON->objToJson($obj, {pretty => 1, indent => 4});



            an obvious improvement would be to accept the filename of the json
            file as a parameter.

            From your error message though it looks like you have html not json.

            good luck




            --- In ydn-javascript@yahoogroups.com, Randall R Schulz <rschulz@...>
            wrote:
            >
            > On Sunday 02 December 2007 18:39, Ask Bjørn Hansen wrote:
            > > On Nov 30, 2007, at 10:50 AM, Randall R Schulz wrote:
            > > > Compact JSON, as most web apps produce it, is very hard to read.
            > > >
            > > > Does anyone here know of a reformatter / pretty-printer for JSON?
            > > > I'd prefer a command-line tool that runs on (or can be compiled
            > > > for) Linux.
            > > > If necessary, I'd use an on-line tool.
            > >
            > > As someone else said, a few words of Perl:
            > >
            > > perl -MJSON -e '$j=JSON->new(pretty=>1); $/=undef; print $j-
            > >
            > > >objToJson($j->jsonToObj(<>))'
            > >
            > > You can either pipe in "ugly" JSON on stdin or specify a file as a
            > > parameter at the end. Stdout will have the pretty JSON.
            >
            > Perl's not my thing.
            >
            > I took your command and used the URL that freyfogle gave to get JSON.pm
            > (and JSON/Parser.pm, which it refers to and without which it will not
            > run) and I when I invoke it (the command above) I get a pile (246 lines)
            > of diagnostics. Here are the first few:
            >
            > Bareword found where operator expected at JSON/Parser.pm line 4,
            near ""stylesheet" href"
            > (Missing operator before href?)
            > Bareword found where operator expected at JSON/Parser.pm line 4,
            near ""/s/style.css" type"
            > (Missing operator before type?)
            > Bareword found where operator expected at JSON/Parser.pm line 7,
            near "</head"
            > (Might be a runaway multi-line // string starting on line 6)
            > (Missing operator before head?)
            >
            >
            > What's the right way to use this stuff? I'm running on Linux (SuSE
            Linux 10.0)
            >
            >
            > > - ask
            >
            >
            > Thanks.
            >
            > Randall Schulz
            >
          • Randall R Schulz
            ... I ll give that a try. ... Definitely not. It s JSON. ... Randall Schulz
            Message 5 of 10 , Dec 3, 2007
            • 0 Attachment
              On Monday 03 December 2007 02:52, freyfogle wrote:
              > this can be greatly simplified, but for clarity I've been fairly
              > verbose:
              >
              > ...

              I'll give that a try.


              > From your error message though it looks like you have html not json.

              Definitely not. It's JSON.


              > good luck


              Randall Schulz
            • Randall R Schulz
              ... Gack. That was complete cockpit error. I tried to manually install the necessary Perl packages. Then I remembered CPAN was a command that handled all the
              Message 6 of 10 , Dec 3, 2007
              • 0 Attachment
                On Monday 03 December 2007 07:34, Randall R Schulz wrote:
                > On Monday 03 December 2007 02:52, freyfogle wrote:
                > > this can be greatly simplified, but for clarity I've been fairly
                > > verbose:
                > >
                > > ...
                >
                > I'll give that a try.
                >
                > > From your error message though it looks like you have html not
                > > json.

                Gack. That was complete cockpit error. I tried to manually install the
                necessary Perl packages. Then I remembered CPAN was a command that
                handled all the server interaction, dependencies and building. The
                reason for all the HTML fragments in the diagnostics I was getting was
                that one of the files I manually retrieved from the CPAN package index
                page was named with a .pm suffix, but was actually an HTML file (the
                information page for that package).


                I'm on the right track now, but Ask's simple command line:

                perl -I$HOME/.cpan/build/JSON-1.15/lib -MJSON -e \
                '$j=JSON->new(pretty=>1); $/=undef;
                print $j->objToJson($j->jsonToObj(<>))'

                Just outputs a large number (135862740). It doesn't seem to matter what
                JSON input I supply...


                However, with only a tiny bit of tweaking (to make it a pure filter, at
                least for now), your script works fine.


                Thanks!


                Randall Schulz
              Your message has been successfully submitted and would be delivered to recipients shortly.