Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day bliako,

I'm assuming that by "long JSON" you're referring the JSON with most of the whitespace removed which, I agree, can be almost impossible to read. In order to make this more readable, you could use a formatter. There's several free ones available; I have "JSON Formatter and Validator bookmarked — I do use it a fair bit but mostly for the validation functionality.

If by "edit the Perl" you're talking about modifying the Perl data structure programmatically, you could use something along the following lines.

#!/usr/bin/env perl

use strict;
use warnings;
use autodie;
use utf8;

use JSON;

my $json_in = 'pm_11115241_uni_greek.json';
my $json_out = 'pm_11115241_uni_greek_edit.json';

_print_json_file($json_in);
my $json_text = read_json($json_in);
my $perl_ref = decode_json $json_text;
_print_perl_json($perl_ref);
edit_perl_json($perl_ref);
_print_perl_json($perl_ref);
write_json($perl_ref, $json_out);
_print_json_file($json_out);

sub read_json {
    my ($file) = @_;

    open my $fh, '<', $file;
    local $/;
    return <$fh>;
}

sub write_json {
    my ($perl, $file) = @_;

    my $json_text = JSON->new->pretty->encode($perl);

    open my $fh, '>:encoding(UTF-8)', $file;
    print $fh $json_text;
}

sub edit_perl_json {
    my ($perl) = @_;

    my $greek_key = 'ΙΚΛΜΝΞΟΠ';
    my $greek_val = 'ικλμνξοπ';

    $perl->{$greek_key} = $greek_val;
}

sub _print_json_file {
    my ($file) = @_;

    print "*** Contents of '$file' ***\n";

    system cat => $file;
}

sub _print_perl_json {
    my ($perl) = @_;

    print "*** Perl from JSON ***\n";

    use open OUT => qw{:encoding(UTF-8) :std};

    for (sort keys %$perl) {
        print $_, ' = ', $perl->{$_}, "\n";
    }
}

Here's a sample run:

$ ./pm_11115241_uni_json_perl.pl
*** Contents of 'pm_11115241_uni_greek.json' ***
{
    "ΑΒΓΔΕΖΗΘ" : "αβγδεζηθ"
}
*** Perl from JSON ***
ΑΒΓΔΕΖΗΘ = αβγδεζηθ
*** Perl from JSON ***
ΑΒΓΔΕΖΗΘ = αβγδεζηθ
ΙΚΛΜΝΞΟΠ = ικλμνξοπ
*** Contents of 'pm_11115241_uni_greek_edit.json' ***
{
   "ΑΒΓΔΕΖΗΘ" : "αβγδεζηθ",
   "ΙΚΛΜΝΞΟΠ" : "ικλμνξοπ"
}

Although <code> tags are generally preferred for code and output, when dealing with Unicode, <pre> tags will not convert your characters to HTML entities. For inline, as opposed to block, markup, I use <tt> tags for the same purpose.

— Ken


In reply to Re: Convert JSON to Perl and back with unicode by kcott
in thread Convert JSON to Perl and back with unicode by bliako

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-18 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found