Darren Ferguson - Weblog

Perl - Current page as JSON

Accessing Umbraco content with Perl and dumping the current node as JSON. Probably best to view source and see the JSON.

Marco Tag

<?PERL_MACRO id="json" pageID="<?UMBRACO_GETITEM field="pageID"/>"></?PERL_MACRO>

Marco Output

PERL FOR UMBRACO: The remote server returned an error: (405) Method Not Allowed. requesting http://www.darren-ferguson.com:80/plex/json.plex

Perl Source

use strict;
use utf8;
use CGI;
use Date::Format;
use XML::XPath;
use XML::Simple;
use JSON;
 
my $query = new CGI;
print $query->header(-charset => 'utf-8');
my $xml = $query->param('contentXml');
my $pageId = $query->param('pageID');
if($xml) {

	my $xp = XML::XPath->new( xml => $xml);
	my $nodeset = $xp->find("//node[\@id='$pageId']/data");
	my $pageData;
	
	foreach my $node ($nodeset->get_nodelist) {
       		
       		$pageData .= XML::XPath::XMLParser::as_string($node);
        }	
	$pageData = "$pageData";
	my $ob = XMLin($pageData);
	print to_json($ob, { pretty => 1 });
	
	
} else {
	print 'no xml specified';
}