To read json data from file, optionally utf8-encoded, into a Perl data structure I do:

use strict; use warnings; # use this to enable script working with utf8 strings use utf8; # use this in order to print utf8 to console binmode STDOUT, ":utf8"; binmode STDERR, ":utf8"; # contains json-encode/decode routines use JSON; # contains utf8 encode/decode routines use Encode; # where json data is: my $jsonfile = 'data.json'; # open file with json my $INP; die "opening '$jsonfile'" unless open $INP, '<:encoding(UTF-8)', $json +file; # slurp file contents my $contents; { local $/ = undef; $contents = Encode::encode 'UTF-8', <$INP> } close + $INP; die "nothing to convert in '$jsonfile'..." unless $contents; # convert json text to perl data structure my $perldata = JSON::decode_json $contents; die "JSON::decode_json() has failed" unless $perldata; # dump the data for debugging use Data::Dumper; # only needed if you want to dump a data structure print Dumper($perldata); # access element of data print "image is ".$perldata->{'image'}."\n"; print "description (has unicode) is ".$perldata->{'description'}."\n"; print "meta/links/self is ".$perldata->{'meta'}->{'links'}->{'self'}." +\n";

bw, bliako


In reply to Re^4: Running JavaScript from within Perl by bliako
in thread Running JavaScript from within Perl by anautismobserver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.