Note that the following does not in any way encode, decode, parse or validate JSON syntax. It just naively pretty-prints JSON-ish stuff that is assumed valid. I'm sure there are modules that would do a much better, more reliable job of this, but you specify that, for some reason, you do not want to use any modules. (Update: Note in particular choroba's comments on this.)

Win8 Strawberry 5.8.9.5 (32) Wed 09/07/2022 7:13:21 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); # for debug my $j = <<'EOJ'; "device": [{"host_info" : { "engine_id":null , "name": null ,"host_group_info" :{"name":null,"remarks":null , "id" : 0 } }} ] EOJ print ">>\n$j<< \n"; my $dent = ' '; my $n_dent = 0; my %pretty = ( '{' => sub { my $d = $dent x ++$n_dent; return qq{$_[0]\n$d}; }, '}' => sub { my $d = $dent x --$n_dent; return qq{\n$d$_[0]}; }, ',' => sub { my $d = $dent x $n_dent; return qq{$_[0]\n$d}; }, ':' => sub { my $d = $dent x $n_dent; return qq{$_[0] }; }, ); $j =~ s{ \s* ([{},:]) \s* } { $pretty{$1}->($1) }xmsge; print ">>\n$j<< \n"; ^Z >> "device": [{"host_info" : { "engine_id":null , "name": null ,"host_group_info" :{"name":null,"remarks":null , "id" : 0 } }} ] << >> "device": [{ "host_info": { "engine_id": null, "name": null, "host_group_info": { "name": null, "remarks": null, "id": 0 } } }] <<

Update: Here's a variation that handles double-quoted strings a little better. All the old caveats still apply.

Win8 Strawberry 5.30.3.1 (64) Wed 09/07/2022 8:14:56 C:\@Work\Perl\monks >perl use 5.010; # needs \K regex extension use strict; use warnings; # use Data::Dump qw(dd); # for debug my $j = <<'EOJ'; "device\"": [{"host_in}fo" : { "eng{ine_id,":null , "name:": null ,"host_group_info\\" :{"na\"me":null,"\"remarks":null , "id" : 0 } "{[:," : "weird", "crazy" : "[{,:" }} ] EOJ print ">>\n$j<< \n"; use constant DENT => ' '; my $n_dent = 0; my %pretty = ( '{' => sub { my $d = DENT x ++$n_dent; return "$_[0]\n$d"; }, '}' => sub { my $d = DENT x --$n_dent; return "\n$d$_[0]\n$d"; }, ',' => sub { my $d = DENT x $n_dent; return "$_[0]\n$d"; }, ':' => sub { my $d = DENT x $n_dent; return "$_[0] "; }, ); my $rx_d_quote = qr{ " [^\\"]* (?: \\. [^\\"]*)* " }xms; $j =~ s{ $rx_d_quote? \K \s* ([{},:]) \s* } { $pretty{$1}->($1) }xmsge; print ">>\n$j<< \n"; ^Z >> "device\"": [{"host_in}fo" : { "eng{ine_id,":null , "name:": null ,"host_group_info\\" :{"na\"me":null,"\"remarks":null , "id" : 0 } "{[:," : "weird", "crazy" : "[{,:" }} ] << >> "device\"": [{ "host_in}fo": { "eng{ine_id,": null, "name:": null, "host_group_info\\": { "na\"me": null, "\"remarks": null, "id": 0 } "{[:,": "weird", "crazy": "[{,:" } } ] <<


Give a man a fish:  <%-{-{-{-<


In reply to Re: Regex replacing Brackets with horizontal tabs and newliners (updated) by AnomalousMonk
in thread Regex replacing Brackets with horizontal tabs and newliners by PerlMonkey22

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.