# Peoplevox XML to JSON conversion script

## Introduction

If you are pulling data from Peoplevox (i.e. your [process flow](https://doc.wearepatchworks.com/product-documentation/process-flows/about-process-flows) includes a [connection shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape) configured with a Peoplevox `GET` endpoint), the payload is generated as XML data.&#x20;

If required, you can convert the XML payload to JSON by adding the script [below](#custom-script-xml-to-json) to a [script shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/script-shape) immediately after the Peoplevox [connection shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape). For example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FIaH83Z0wu9qzBEOeoFXI%2Fpvx%20xml%20conversion%20flow.png?alt=media&#x26;token=583f79ba-3447-417a-b5a9-111b06634035" alt="" width="268"><figcaption></figcaption></figure></div>

## Custom script - XML to JSON

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

{% code lineNumbers="true" %}

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

{% endcode %}
