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
This is in my importer class
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"}]
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
}),
"[{\"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
Still can't find a solution to this problem.
Tried it like so now, but no succes always imports it as
I am working on it for days now, but no clue how to get the array properly imported.
I tried it like so
like so
tried to cast it in the
tried it also with
it always gets imported the same
Please if someone has experience with it and could help me.
"[{\"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;
}
}
ExportColumn::make('operating_expenses'),
ExportColumn::make('operating_expenses'),
ExportColumn::make('operating_expenses')
->listAsJson(),
ExportColumn::make('operating_expenses')
->listAsJson(),
beforeValidate() function
beforeValidate() function
castStateUsing()
castStateUsing()
"[{\"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\"}]"