Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Created July 24, 2025 15:37
Show Gist options
  • Select an option

  • Save codearachnid/816586134dbbd679cd0b0b03abe50c44 to your computer and use it in GitHub Desktop.

Select an option

Save codearachnid/816586134dbbd679cd0b0b03abe50c44 to your computer and use it in GitHub Desktop.
Simple parser to convert csv export from brightdata into json output for normalization
<?php
function parseCsvRowToJson($csvRow) {
$keys = [
'url', 'domain', 'country_code', 'model_number', 'sku', 'product_id', 'product_name', 'manufacturer',
'final_price', 'initial_price', 'currency', 'in_stock', 'root_category', 'category', 'category_tree',
'rating', 'reviews_count', 'answered_questions_count', 'main_image', 'image_urls', 'features',
'dimensions', 'weight', 'details', 'warranty', 'related_documents', 'top_reviews', 'answered_questions',
'upc', 'upcgtin13', 'price_range', 'availability_text', 'promotion_fulltext', 'available_to_delivery',
'zipcode', 'store_name', 'store_number', 'description', 'timestamp', 'input', 'discovery_input',
'error', 'error_code', 'warning', 'warning_code'
];
$data = str_getcsv($csvRow);
$result = [];
foreach ($keys as $index => $key) {
$value = $data[$index] ?? '';
// Handle specific data types
switch ($key) {
case 'final_price':
case 'initial_price':
case 'rating':
$result[$key] = floatval($value);
break;
case 'reviews_count':
case 'answered_questions_count':
case 'available_to_delivery':
case 'zipcode':
$result[$key] = intval($value);
break;
case 'in_stock':
$result[$key] = $value === 'TRUE';
break;
case 'root_category':
case 'category':
case 'input':
case 'discovery_input':
$result[$key] = $value ? json_decode($value, true) : [];
break;
case 'category_tree':
case 'image_urls':
case 'features':
case 'dimensions':
case 'details':
case 'related_documents':
case 'top_reviews':
case 'answered_questions':
$result[$key] = $value ? json_decode($value, true) : [];
break;
default:
$result[$key] = $value;
break;
}
}
return json_encode($result, JSON_PRETTY_PRINT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment