How to import json/array from csv?

No matter what I try I can't import json/array properly from a csv file. This is how it looks in file
[{"cost":0,"name":"Home inspection","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Appraisal","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Loan Points","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Lender Fees","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Title & Escrow Fees","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Transfer Taxes","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Attorney Fees","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Wholesaler Fees","in_loan":0,"percent":0,"cost_type":"set-amount"}]
[{"cost":0,"name":"Home inspection","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Appraisal","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Loan Points","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Lender Fees","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Title & Escrow Fees","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Transfer Taxes","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Attorney Fees","in_loan":0,"percent":0,"cost_type":"set-amount"},{"cost":0,"name":"Wholesaler Fees","in_loan":0,"percent":0,"cost_type":"set-amount"}]
This is in my importer class
ImportColumn::make('purchase_costs')
->castStateUsing(function (string $state): ?array {
if (blank($state)) {
return null;
}

return json_decode($state, true); // Decode JSON string to an associative array
}),
ImportColumn::make('purchase_costs')
->castStateUsing(function (string $state): ?array {
if (blank($state)) {
return null;
}

return json_decode($state, true); // Decode JSON string to an associative array
}),
This is how it looks in my DB
"[{\"cost\":0,\"name\":\"Home inspection\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Appraisal\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Loan Points\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Lender Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Title & Escrow Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Transfer Taxes\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Attorney Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Wholesaler Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"}]"
"[{\"cost\":0,\"name\":\"Home inspection\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Appraisal\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Loan Points\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Lender Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Title & Escrow Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Transfer Taxes\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Attorney Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Wholesaler Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"}]"
1 Reply
kekitek
kekitekOP6mo ago
Still can't find a solution to this problem. Tried it like so now, but no succes always imports it as
"[{\"cost\":0,\"name\":\"Home inspection\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Appraisal\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Loan Points\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Lender Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Title & Escrow Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Transfer Taxes\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Attorney Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Wholesaler Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"}]"
"[{\"cost\":0,\"name\":\"Home inspection\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Appraisal\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Loan Points\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Lender Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Title & Escrow Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Transfer Taxes\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Attorney Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Wholesaler Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"}]"
protected function beforeValidate(): void
{
$this->data['purchase_costs'] = $this->parseCosts($this->data['purchase_costs']);
$this->data['rehab_costs'] = $this->parseCosts($this->data['rehab_costs']);
$this->data['other_income'] = $this->parseCosts($this->data['other_income']);
}

/**
* Parse the costs.
*/
protected function parseCosts($costs)
{
if (empty($costs)) {
return null;
}

return $this->tryParseCosts($costs);
}

/**
* Try to parse the costs.
*/
protected function tryParseCosts($costs)
{
try {
return json_decode($costs, true);
} catch (\Exception $e) {
return null;
}
}
protected function beforeValidate(): void
{
$this->data['purchase_costs'] = $this->parseCosts($this->data['purchase_costs']);
$this->data['rehab_costs'] = $this->parseCosts($this->data['rehab_costs']);
$this->data['other_income'] = $this->parseCosts($this->data['other_income']);
}

/**
* Parse the costs.
*/
protected function parseCosts($costs)
{
if (empty($costs)) {
return null;
}

return $this->tryParseCosts($costs);
}

/**
* Try to parse the costs.
*/
protected function tryParseCosts($costs)
{
try {
return json_decode($costs, true);
} catch (\Exception $e) {
return null;
}
}
I am working on it for days now, but no clue how to get the array properly imported. I tried it like so
ExportColumn::make('operating_expenses'),
ExportColumn::make('operating_expenses'),
like so
ExportColumn::make('operating_expenses')
->listAsJson(),
ExportColumn::make('operating_expenses')
->listAsJson(),
tried to cast it in the
beforeValidate() function
beforeValidate() function
tried it also with
castStateUsing()
castStateUsing()
it always gets imported the same
"[{\"cost\":0,\"name\":\"Home inspection\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Appraisal\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Loan Points\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Lender Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Title & Escrow Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Transfer Taxes\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Attorney Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Wholesaler Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"}]"
"[{\"cost\":0,\"name\":\"Home inspection\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Appraisal\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Loan Points\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Lender Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Title & Escrow Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Transfer Taxes\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Attorney Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"},{\"cost\":0,\"name\":\"Wholesaler Fees\",\"in_loan\":0,\"percent\":0,\"cost_type\":\"set-amount\"}]"
Please if someone has experience with it and could help me.
Want results from more Discord servers?
Add your server