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

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

Expand Messages
  • 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 1 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 2 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 3 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 4 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 5 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.