To view this content in our official product documentation, click here.
Introduction
If you're building a process flow with Peoplevox as a target connection, you must create a setData script and then - in your process flow - apply this script immediately before the target connection.
Creating a setData script
Step 1\ From the Patchworks dashboard, select scripts from the left-hand navigation menu.
Step 2\ Click the create script button:

Step 3\ In the name field, type the following:
PVX SetDataStep 4\ In the description field, type the following:
Convert flat Json payload to csv SetData formatStep 5\ Click in the language field and select PHP 8.1:

Step 6\ Click the create button:

Step 7\ The script is created and opened in edit mode - select and remove the placeholder code:

Step 8\ Paste in the code below.
SetData script code
{% code lineNumbers="true" %}
<?php
function handle($data) {
$payload = json_decode($data['payload'], true);
$headers = [];
$lines = [];
foreach ($payload as $payloadItem) {
$lineHeaders = array_keys($payloadItem);
if (empty($headers)) {
$headers = $lineHeaders;
} else {
$keyDiff = array_diff_key($lineHeaders, $headers);
foreach ($keyDiff as $key) {
$headers[] = $key;
}
}
$lines[] = $payloadItem;
}
$final = [sprintf('"%s"', implode('","', $headers))];
foreach ($lines as $line) {
$finalLine = [];
foreach ($headers as $header) {
if (array_key_exists($header, $line) && !empty($line[$header])) {
$finalLine[] = trim(str_replace([',', "\n", "\r", '"', "'"], '', $line[$header]));
} else {
$finalLine[] = '';
}
}
$final[] = sprintf('"%s"', implode('","', $finalLine));
}
$finalBody = implode("\n", $final);
$data['payload'] = $finalBody;
return $data;
};
Step 9\ Click the save and deploy button:

You don't need to do anything with the **setData** script right now. However, if you build a process flow with Peoplevox as a **target** connection, **you must add a custom script shape and apply this setData script immediately before the target connection shape**. For more information please see: Using the Peoplevox setData script in process flows.
Comments
0 comments
Please sign in to leave a comment.