The Peoplevox setData script
PreviousDescartes Peoplevox (prebuilt connector)NextUsing the Peoplevox setData script in process flows
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
PVX SetDataConvert flat Json payload to csv SetData format <?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;
};