Peoplevox XML to JSON conversion script
PreviousThe Peoplevox re-authentication response scriptNextUsing the Peoplevox search filter when pulling data
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
<?php
/**
* Handler function.
*
* @param array $data [
* 'payload' => (string|null) the payload as a string|null
* 'variables' => (array[string]string) any variables as key/value
* 'meta' => (array[string]string) any meta as key/value
* ]
*/
function handle($data)
{
$xml = simplexml_load_string($data['payload']);
$body = $xml->xpath("//*[local-name()='Detail']");
$jsonData = [];
if (count($body) === 1) {
$doc = dom_import_simplexml($body[0]);
$content = $doc->nodeValue;
$rows = explode("\n", $content);
$headers = array_shift($rows);
$headers = str_getcsv($headers);
foreach ($rows as $row) {
if (strlen($row)) {
$jsonData[] = array_combine(
$headers,
str_getcsv($row)
);
}
}
}
$data['payload'] = json_encode($jsonData);
return $data;
}