Peoplevox XML to JSON conversion script

Introduction

If you are pulling data from Peoplevox (i.e. your process flow includes a connection shape configured with a Peoplevox GET endpoint), the payload is generated as XML data.

If required, you can convert the XML payload to JSON by adding the script below to a script shape immediately after the Peoplevox connection shape. For example:

Custom script - XML to JSON

The script below will convert any Peoplevox XML payload, irrespective of the entity type being pulled.

<?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;
}

Last updated