update: meh, I completely read right past where you said you wanted to process in XML. I digress.../update

As Corion mentioned, this is JSON, and is exceptionally common for web API returns. Here's how you can import it into a Perl structure and get at it.

use warnings; use strict; use Data::Dumper; use JSON::XS; my $json; { local $/; $json = <DATA>; } my $perl = decode_json $json; for my $href (@$perl){ print "vendor: $href->{Header}{VendorName}\n"; for my $item (@{ $href->{Items} }){ print "\track num: $item->{RackNumber}\n"; } } __DATA__ ...paste the JSON string here

output:

vendor: Some Business rack num: 3127824 rack num: 3127825 vendor: Some Business rack num: 3127828 rack num: 3128353 rack num: 3128796 rack num: 3128797

Now, a couple of notes. I'm going to rework it with your actual code, and explain what's happening. You don't need the special $/ block in your case, nor the __DATA__ section:

use warnings; use strict; use HTTP::Request::Common qw(GET); use JSON::XS; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $endpoint = 'http://blah'; my $req = HTTP::Request->new('GET'); $req->url($endpoint); my $resp = $ua->request($req); my $json = $resp->content; my $perl = decode_json $json; # it's an array of hashes of hashes (AoHoH). We need to # access it like this: for my $href (@$perl){ # grabbed each hash reference from the top-level array # ...now, print the vendor's name print "vendor: $href->{Header}{VendorName}\n"; for my $item (@{ $href->{Items} }){ # each top level href has a Header href, and many # Item hrefs inside of an array ref. Here, we're # looping over each Item, and doing something print "\track num: $item->{RackNumber}\n"; } }

In reply to Re: Consuming A Web Service by stevieb
in thread Consuming A Web Service by pkupnorth

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.