Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #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';

my $json_text = read_json($json_in);
my $perl_ref = decode_json $json_text;
write_json($perl_ref, $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:

$ ./
*** 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

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (7)
    As of 2020-07-06 13:24 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found