[
{"cxbrandid":"100158:100159","cxbrandname":"APPLE, ORANGE & BANANA:JUICE JUICE"},
{"cxbrandid":"3000","cxbrandname":"JUNGLE, FOREST"}
]
desired output json file:
[{"cxbrandid":["100158","100159"],"cxbrandname":["APPLE, ORANGE & BANANA","JUICE JUICE"]},{"cxbrandid":["3000"],"cxbrandname":["JUNGLE, FOREST"]}]
Perl script to do that:
========================================================================
use JSON;
my $json;
{
local $/; #Enable 'slurp' mode
open my $fh, "<", "t.json";
$json = <$fh>;
close $fh;
}
my $data = decode_json($json);
#count number of elements in array and minus one
$t = scalar(@{$data});
$t = $t - 1;
for (my $i=0; $i <= $t; $i++) {
$cxbrandname = $data->[$i]->{'cxbrandname'};
$cxbrandid = $data->[$i]->{'cxbrandid'};
my $brandidtable=[];
my $brandnametable=[];
@cxbrandidarray=split(/:/, $cxbrandid);
@cxbrandnamearray=split(/:/, $cxbrandname);
foreach my $val (@cxbrandidarray) {
push @{$brandidtable}, $val;
}
foreach my $val (@cxbrandnamearray) {
push @{$brandnametable}, $val;
}
$data->[$i]->{'cxbrandid'}= $brandidtable;
$data->[$i]->{'cxbrandname'}= $brandnametable;
}
open my $fh, ">", "data_out.json";
print $fh encode_json($data);
close $fh;
No comments:
Post a Comment