How to import a json column #3816
-
I'm trying to import a file that contains json data in some columns and this data needs to be imported into JSONB fields in PostgreSQL. Json data example: However, when the file gets imported, the data imported appears as follows in the database: I need the data imported EXACTLY as how the example is provided. Is there any way to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Found the solution, basically, the following things need to be considered:
Laravel models convert data to arrays to insertion, but if the fields in DB are JSON fields the model will automatically serialize the values Reference: https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting Example of my code: /** private function isJson($string): bool |
Beta Was this translation helpful? Give feedback.
Found the solution, basically, the following things need to be considered:
Laravel models convert data to arrays to insertion, but if the fields in DB are JSON fields the model will automatically serialize the values
Reference: https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting
Example of my code:
/**
* @throws \JsonException
*/
public function model(array $row)
{
$this->affectedRows++;
$parameters = [];
$i = 0;
foreach ($this->columns as $column => $value) {
if ($value) {
$parameters[$column] = $value;
} elseif ($this->isJson($row[$i])) {
$parameters[$column] = json_deco…