Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Write json structure to a file

by anonymized user 468275 (Curate)
on Aug 13, 2015 at 12:34 UTC ( [id://1138420]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Write json structure to a file
in thread Write json structure to a file

++Corion, and so if you want ordered pairs (same rules for json as Perl), the most obvious treatment is to replace that hash with an array of little hashes, e.g.:
use strict; use warnings; binmode STDOUT, ":utf8"; use utf8; use JSON; my $json ={ 'book' => [ {'title' => 'smth'}, {'num_page' => 234} ] }; open my $fh, ">", "data_out.json"; print $fh encode_json($json); close $fh;
Alternatively, you could sort the hash keys on the receiving end in Javascript into a particular order (put the key names in another hash with the sort positions as values for use in a sort function in Javascript) but that would be outside the scope of the monastery.

One world, one people

Replies are listed 'Best First'.
Re^4: Write json structure to a file
by Anonymous Monk on Oct 20, 2017 at 05:26 UTC

    Hi! I'm getting this error while using binmode STDOUT, ":utf8". Please help! :)

    Failed to require abc.pm because Can't locate object method "BINMODE" via package "OutputTee" at /home/perl.abc.pm line 33.

      Your package OutputTee does seem to implement a tied filehandle but seems to not properly implement the BINMODE method.

      You will have to fix OutputTee.pm to do the right thing. Likely, that is calling binmode on all its filehandles:

      sub BINMODE { my( $self, @args ) = @_; binmode $self->{fh1}, @args; binmode $self->{fh2}, @args; }

        Thanks for your response! :)

      Can't locate object method "BINMODE"

      binmode is a function, so it should be in lowercase.

      $ perl -e 'binmode STDOUT, ":utf8";' $ perl -v This is perl 5, version 20, subversion 3 (v5.20.3) built for x86_64-li +nux-thread-multi (with 16 registered patches, see perl -V for more detail) Copyright 1987-2015, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. $

      Does the first command work for you? If not, run the second and see how old your perl is.

      If the first command does work for you (ie. doesn't throw an error) then you'll have to provide an SSCCE to illustrate the precise circumstance of your problem. (and probably start a new topic since it's not really specific to JSON anymore)

        Thanks for your response! Will do as you suggested. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found