How to save JSON array to MySQL database?

MySQLMySQLi Database

For this, you can use JSON data type from MySQL. Let us first create a −

mysql> create table DemoTable1438
   -> (
   -> EmployeeDetails json
   -> );
Query OK, 0 rows affected (5.97 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1438 values('[{"EmployeeId":"EMP-101","EmployeeName":"Chris"},{"EmployeeId":"EMP-102","EmployeeName":"David"},{"EmployeeId":"EMP-103","EmployeeName":"Sam"}]');
Query OK, 1 row affected (0.22 sec)

Display all records from the table using select −

mysql> select * from DemoTable1438;

This will produce the following output −

+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EmployeeDetails                                                                                                                                            |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [{"EmployeeId": "EMP-101", "EmployeeName": "Chris"}, {"EmployeeId": "EMP-102", "EmployeeName": "David"}, {"EmployeeId": "EMP-103", "EmployeeName": "Sam"}] |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
raja
Published on 12-Nov-2019 06:52:18
Advertisements