forked from stevemeier/cefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2json.pl
executable file
·55 lines (43 loc) · 1.25 KB
/
xml2json.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl
#
# This script converts an XML input file to JSON output
# Pipe the output to `jq -S` for best results
# Author: Steve Meier
use strict;
use warnings;
use Getopt::Long;
use JSON qw(to_json);
use XML::Simple;
my ($xmlfile, $xml);
my $getopt = GetOptions('in=s' => \$xmlfile);
if (not(defined($xmlfile))) {
print "ERROR: Please define --in <xmlfile>\n";
exit 1;
}
if (-r $xmlfile) {
$xml = XMLin($xmlfile, ForceArray => [ qw(/keywords/ os_arch os_release packages) ] );
} else {
print "ERROR: Could not read $xmlfile\n";
exit 1;
}
foreach my $advisory (sort(keys(%{$xml}))) {
my $newkey;
if ($advisory =~ /^CE/) {
$newkey = $advisory;
# Use proper advisory name
$newkey =~ s/--/:/;
${$xml}{$advisory}{'id'} = $newkey;
# ForceArray should take care of this,but it doesn't ¯\_(ツ)_/¯
if (defined(${$xml}{$advisory}{'keywords'})) {
push(@{${$xml}{$advisory}{'keywords2'}}, ${$xml}{$advisory}{'keywords'});
${$xml}{$advisory}{'keywords'} = ${$xml}{$advisory}{'keywords2'};
delete(${$xml}{$advisory}{'keywords2'});
}
# Stick errata into an array
push(@{${$xml}{'advisories'}}, ${$xml}{$advisory});
# Remove original data
delete ${$xml}{$advisory};
}
}
print to_json($xml);
exit;