Hi , I am trying to convert below data to json : Once its store in json ,will send data to httprequest using LWP::UserAgent. Also getting this string "'remedy_queue' => bless( [], 'XML::XPath::NodeSet' )," in my records.How to remove this?
$VAR1 = { 'appname' => 'IPT(957)', 'event_time' => '2017-06-06 15:37:17', 'line' => 1, 'ticketnum' => 'INC000015345', 'remedy_queue' => 'Coll@borate-L3', 'severity' => 'critical' }; $VAR2 = { 'appname' => 'Instant Messaging - Lync External(2667)', 'remedy_queue' => bless( [], 'XML::XPath::NodeSet' ), 'event_time' => '2017-04-07 13:54:51', 'line' => 2, 'ticketnum' => 'INC0000134513', 'severity' => 'critical' }; $VAR3 = { 'severity' => 'critical', 'event_time' => '2017-04-07 13:54:50', 'line' => 3, 'ticketnum' => 'INC00001847651', 'remedy_queue' => 'Global Service Desk', 'appname' => 'Instant Messaging - Lync External(2667)' };
Perl Code: Please do optimized below CODE if required ..
##!/usr/bin/perl use warnings; use strict; use XML::XPath; use Data::Dumper; use LWP::UserAgent; use JSON; use DateTime; use DateTime::Duration; use DateTime::Format::Duration; use DateTime::Format::Strptime; use Cpanel::JSON::XS qw(encode_json); my $xml = 'events.xml'; my $xp = XML::XPath->new(filename => $xml); my $nodeset = $xp->findnodes('//event'); my $records = []; my $count=0; foreach my $node ($nodeset->get_nodelist) { $count ++; my $severity = $xp->find('./severity', $node); $severity =~ s/\n//; my $ticketnum = $xp->find("./custom_attribute_list/custom_att +ribute[normalize-space(name)='RemedyIncident']/value", $node); $ticketnum=~ s/\n//; my $timereceived = $xp->find('./time_first_received', $node); $timereceived=~ s/\n//; my $service_name = $xp->find("./custom_attribute_list/custom_a +ttribute[normalize-space(name)='Service Name']/value", $node); $service_name=~ s/\n//; my $ssrid = $xp->find("./custom_attribute_list/custom_attribut +e[normalize-space(name)='SSRID']/value", $node); $ssrid=~ s/\n//; my $remedyqueue = $xp->find("./custom_attribute_list/custom_at +tribute[normalize-space(name)='RemedyQueue']/value", $node); $remedyqueue=~ s/\n//; #Format $timereceived $timereceived =~ s/T/ /; #removing the T $timereceived =~ s/\.\d+Z//; #removing the Z my $dtnow = DateTime->now; my $strp = DateTime::Format::Strptime->new(on_error=>'croak',p +attern => '%Y-%m-%d %H:%M:%S', time_zone=>'UTC'); my $dtevent = $strp->parse_datetime($timereceived); my $diff_sec = $dtnow->subtract_datetime_absolute($dtevent)->i +n_units('seconds'); my $diff_hours = $diff_sec/(60*60); #print "$diff_sec s / $diff_hours h\n"; #if ($diff_hours>4000) { ##//TODO:delete after testing the out +put if ($diff_hours>2) { push @$records, { line => $count, severity => $severity, ticketnum => $ticketnum, appname => $service_name . "(" . $ssrid . ")", remedy_queue => $remedyqueue, event_time => $timereceived, } } } print Dumper(@$records); # my $uri = 'http://testing/dims/line/tags/availability'; # my $json = '{ # "line":"4", # "status":"Critical", # "appname":"aaa", # "ticketnumber":"INC0012312312312", # "eventage":"0.5 h", # "specialist":"Guru" # }'; # my $req = HTTP::Request->new( 'POST', $uri ); # $req->header( 'Content-Type' => 'application/json'); # $req->content( $json ); # my $lwp = LWP::UserAgent->new; # $lwp->request( $req );

In reply to array data to json by snehit.ar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.