I´m making a project where my script tests our JSON API. I have one functional version:
use strict;
use warnings;
use JSON;
use JSON::Parse 'parse_json';
use Data::Dumper qw(Dumper);
my $content = `curl --silent -k -u admin:pass https://url/api/v2/GetDe
+viceInfo?$ARGV[0]`;
my $decoded_json = decode_json($content);
my $data = Dumper $decoded_json;
foreach my $line($data) {
print "$line\n";
}
The second part is to get the same output with all the Objects and Arrays highlighted but without any modules. I´ve got this so far:
my $content = `curl --silent -k -u admin:pass https://url/api/v2/GetDe
+viceInfo?$ARGV[0]`;
$content =~ tr/"/ /;
$content =~ tr/,/\n/;
print "$content\n";
The $content variable is an JSON API. The whole JSON is an Array, with objects in it, and other Arrays in those objects. The [] are for the Arrays and {} for Objects.
Current Output of my script is:
"device":[{ host_info: { engine_id: null
name: null
host_group_info: { name: null
remarks: null
id: 0}
...
The objective is to make it look like below:
"device": [{
"host_info": {
"engine_id": null,
"name": null,
"host_group_info": {
"name": null,
"remarks": null,
"id": 0
}
}
...
I strugle to implement a tab after each opening bracket and reverse the tab after each closing bracket. Maybe someone has got even a simpler solution than that?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.