Building ordered JSON in perl

I have a bit of a fiddle with JSON and D3 trying to present bubble-style disk usage information.

Based on this, at the initial stage: http://bl.ocks.org/mbostock/4063269

JSON is pretty simple - hierarchical data looks something like this:

http://bl.ocks.org/mbostock/raw/4063530/flare.json

{
 "name": "flare",
 "children": [
  {
   "name": "analytics",
   "children": [
    {
     "name": "cluster",
     "children": [
      {"name": "AgglomerativeCluster", "size": 3938},
      {"name": "CommunityStructure", "size": 3812},
      {"name": "HierarchicalCluster", "size": 6714},
      {"name": "MergeEdge", "size": 743}
     ]
    }
   ]
  }
 ]
}

Now, what I'm trying to do is get a report quotasfrom a NetApp form - in XML format.

I have several XML files on a single server that look something like this:

<quotas>
  <quota>
    <quota-target>/vol/vol1/qtree1</quota-target>
    <volume>vol1</volume>
    <disk-used>554444</disk-used>
    <disk-limit>2000000</disk-limit>
  </quota>
  <quota>
    <quota-target>/vol/vol1/qtree2</quota-target>
    <volume>vol1</volume>
    <disk-used>1235655</disk-used>
    <disk-limit>2000000</disk-limit>
  </quota>
  <quota>
    <quota-target>/vol/vol2/qtree1</quota-target>
    <volume>vol2</volume>
    <disk-used>987664</disk-used>
    <disk-limit>2000000</disk-limit>
  </quota>
</quotas>

What I'm trying to do is compile some JSON for use with D3, which are hierarchical:

  • website
  • Server
  • volume
  • Target quota
  • disk used

I am doing fine with the foreach loop:

#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
use JSON;

my %sites = (
    'site1' => [qw ( servera serverb )],
    'site2' => [qw ( s2serverc s2serverd)],
);

my $data;
$data->{'name'} = "quotas";
foreach my $sitename ( keys %sites ) {
    my $site = { 'name' => $sitename };
    push( @{ $data->{'children'} }, $site );
    foreach my $server ( @{ $sites{$sitename} } ) {
        my $server = { 'name' => $server };
        push( @{ $site->{'children'} }, $server );

        $twig->parsefile("$server.quotas.xml");
        foreach my $quota ( $twig->get_xpath('//quota') ) {
            push(
                @{ $server->{'children'} },
                {   'name' => $quota->first_child_text('quota-target'),
                    'size' => $quota->first_child_text('disk-used')
                }
                )

        }
    }
}

open( my $output, ">", "quotas.json" ) or die $!;
print {$output} to_json( $data, { 'pretty' => 1 } );
close($output);

It works widely and produces beautiful pictures.

However, I have two problems:

JSON , . - - , JSON? ( )

- , "" node, , children foreach . , :

  • get_xpath('//volume') .
  • per volume , ( xpath ?)
  • "" , "" children JSON.

- ?

, .

$stuff{$site}{$server}{$volume}{$qtree} = $size; 

JSON (, , ).

+4
2

, JSON?

, . JSON .

/.

, , .

, , JSON.pm sort_by. , .

+1

" " -, "" , .

0

All Articles