Reputation: 159
How can I use perl to process JSON, so that instead of one line several lines are output as a structure?
my $json_string = '{"status":"success","answer":{"status":"success","result":{"full_fqdn":"example.com","result":"blablabla","php_version":"8.0","cgi":"disabled"}}}';
I want to get a result like the one on this output:
"status": "success",
"answer": {
"status": "success",
"result": {
"full_fqdn": "example.com",
"result": "blablabla",
"php_version": "8.0",
"cgi": "disabled"
}
}
I don't quite understand the proper use of JSON to convert the output this way. Thank you!
Upvotes: 0
Views: 511
Reputation: 241948
JSON::PP is a core module. The pretty
method enables the indenting and spaces around colons. You can control the details with indent
, indent_length
, space_before
, and space_after
.
#!/usr/bin/perl
use warnings;
use strict;
use JSON::PP;
my $json_string = '{"status":"success","answer":{"status":"success","result":{"full_fqdn":"example.com","result":"blablabla","php_version":"8.0","cgi":"disabled"}}}';
my $j = 'JSON::PP'->new->pretty;
print $j->encode($j->decode($json_string));
If you need faster processing, install Cpanel::JSON::XS from CPAN.
Upvotes: 3