# Peoplevox XML to JSON conversion script

## Introduction

If you are pulling data from Peoplevox (i.e. your [process flow](/product-documentation/process-flows/about-process-flows.md) includes a [connection shape](/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape.md) 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](/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/script-shape.md) immediately after the Peoplevox [connection shape](/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape.md). For example:

<div align="left"><figure><img src="/files/lFRSrYeMMHYjvsiEM6Hq" 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/patchworks-connectors/descartes-peoplevox-prebuilt-connector/peoplevox-xml-to-json-conversion-script.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
