将JSON哈希映射展平为perl

时间:2017-07-18 22:41:08

标签: arrays json perl hash hashmap

我是数据结构的新手,我对此有疑问,

我有一个JSON文件

    {
    "COMP1":[
        {
            "machines"      :   "xyz",
            "logLocation"   :   "a.log",
            "duration"      :   "1h",
            "network"       :   "prefix1",
            "searchString"  :   [
                "ERR",
                "CRIT",
                "WARN"

            ],
            "ignoreSearch"  :   [
                "DEFERRED",
                "ABCCRITXYZ"
            ]
        },
        {
            "machines"      :   "sql2",
            "logLocation"   :   "a.log",
            "duration"      :   "1d",
            "network"       :   "imcr",
            "searchString"  :   [
                "ERR",
                "CRIT",
                "WARN"

            ],
            "ignoreSearch"  :   [
                "DEFERRED",
                "ABCCRITXYZ"
            ]
        }
   ],
    "COMP2":[
        {
            "machines"      :   "sql",
            "logLocation"   :   "a.log",
            "duration"      :   "1h",
            "network"       :   "prefix-1",
            "searchString"  :   [
                "ERR",
                "CRIT",
                "WARN"

            ],
            "ignoreSearch"  :   [
                "DEFERRED",
                "ABCCRITXYZ"
            ]
        }
    ]
}

我希望根据“searchString”

数组将其展平为数组
[
   {


 "comp"          : "comp1",
 "machines"      :   "xyz",
 "logLocation"   :   "a.log",
 "duration"      :   "1h",
 "network"       :   "prefix1",
 "searchString"  :    "ERR",
 "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

  {
    "comp"          : "comp1",
    "machines"      :   "xyz",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "prefix1",
    "searchString"  :   "CRIT",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

   {
    "comp"          : "comp1",
    "machines"      :   "xyz",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "prefix1",
    "searchString"  :   "WARN",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    }
],

[
   {
    "comp"          : "comp1",
 "machines"      :   "sql2",
 "logLocation"   :   "a.log",
 "duration"      :   "1h",
 "network"       :   "imcr",
 "searchString"  :    "ERR",
 "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

  {
    "comp"          : "comp1",
    "machines"      :   "sql2",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "imcr",
    "searchString"  :   "CRIT",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

   {
    "comp"          : "comp1",
    "machines"      :   "xyz",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "prefix1",
    "searchString"  :   "WARN",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    }
]

等等, 如何存储它们以便我以后可以单独访问它们? 对于comp2也是如此。 抱歉我的缩进,我在这里问的不多。

2 个答案:

答案 0 :(得分:2)

{{1}}

答案 1 :(得分:0)

我做过类似的事情。欢迎您提出意见和建议。 还要感谢@ikegami和@zdim的headstarts。

use JSON qw( decode_json encode_json from_json to_json );


my $component = decode_json($json);

sub dclone { from_json(to_json($_[0])) }


my @grouped_by_comp;
my @testData;
foreach my $comp (keys %$component){                                     # $comp is an array reference , keys %$component gives an array reference 
    foreach my $test_map (@{$component->{$comp}}){                       #@{$component->{$comp}} is a hash reference in array 
        my $searchStrings = $test_map->{searchString};                      #$test_map->{searchString} is an array reference within a hash 
        foreach my $search ( @$searchStrings ){
            my %tmp_map = %$test_map;
            $tmp_map{component} = $comp;
            $tmp_map{searchString} = $search;
            $tmp_map{ignoreSearch} = [@{$test_map->{ignoreSearch}}];
            push @testData,\%tmp_map;
        }
    }
}
print Dumper(\@testData);