1

I'm unable to import .json file as a table in Mysql 5.7; All rows remain blank. I'm not sure where the error is.

I'm using the following json file stored under /home/name/json_data/sample.json

{
    "price": null,
    "sale_list": [
        {
            "buyer": "SmackMe089",
            "date": "April 29th 2019 21:20:50",
            "id": "1234",
            "item_desc": ""
        }
}

When I attempt to import it into mysql 5.7 using the following sql file nothing is imported.

file.sql:

CREATE TABLE example_table (
         id INT NOT NULL AUTO_INCREMENT,
         json_data JSON NOT NULL,
         PRIMARY KEY (id)
);

LOAD DATA INFILE '/home/path/json_data/sample.json' INTO TABLE example_table (json_data);

Attempt to import into mysql: mysql --host=host_ip -u root -p db_name < /home/path/data/file.sql

4
  • You can't use LOAD DATA in MySQL 5.7 to import JSON. Starting with MySQL 8+ there is a new shell tool util.importJson which can directly parse a JSON file into a table schema. If you have a persistent need for this, maybe consider upgrading. May 1, 2019 at 1:30
  • I'm referencing mysqlserverteam.com/… I'm aware sql 8.0 has a nice shiny mysqlsh util to import json. I'm looking to work with what I have before migrating though and was referencing - mysqlserverteam.com/… May 1, 2019 at 1:52
  • Side note - @TimBiegeleisen - load data is supported so Im little confused what your rational is - dev.mysql.com/doc/refman/5.7/en/load-data.html May 1, 2019 at 2:06
  • Yes, LOAD DATA is supported, but for CSV, not JSON, files. May 1, 2019 at 3:16

2 Answers 2

1

A couple of options are:

  1. 13.2.6 LOAD DATA Syntax
  2. 7.2 JSON Import Utility

Test:

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.26 MySQL Community Server (GPL)

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.26    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS `test`.`example_table`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `test`.`example_table` (
    ->   `id` INT NOT NULL AUTO_INCREMENT,
    ->   `json_data` JSON NOT NULL,
    ->   PRIMARY KEY (`id`)
    -> );
Query OK, 0 rows affected (0.01 sec)

1. LOAD DATA

File: /path/to/file/sample.json

{"price": null, "sale_list": [{"buyer": "SmackMe089", "date": "April 29th 2019 21:20:50", "id": "1234", "item_desc": ""}]}

MySQL Command-Line Client

mysql> LOAD DATA INFILE '/path/to/file/sample.json'
    -> INTO TABLE `test`.`example_table` (`json_data`);
Query OK, 1 row affected (0.01 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

mysql> SELECT `id`, `json_data`
    -> FROM `test`.`example_table`\G
*************************** 1. row ***************************
       id: 1
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
1 row in set (0.00 sec)

2. JSON Import Utility

File: /path/to/file/sample.json

{
    "price": null,
    "sale_list": [
        {
            "buyer": "SmackMe089",
            "date": "April 29th 2019 21:20:50",
            "id": "1234",
            "item_desc": ""
        }
    ]
}

MySQL Shell: As @TimBiegeleisen says, you can use MySQL Shell (even with MySQL 5.7), but you must activate X Plugin:

$ mysqlsh --sql
MySQL Shell 8.0.16

Your MySQL connection id is 2 (X protocol)
Server version: 5.7.26 MySQL Community Server (GPL)
No default schema selected; type \use <schema> to set one.

MySQL 127.0.0.1:15726+ SQL > SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.26    |
+-----------+
1 row in set (0.0004 sec)

MySQL 127.0.0.1:15726+ SQL > SELECT `id`, `json_data`
                          -> FROM `test`.`example_table`\G
*************************** 1. row ***************************
       id: 1
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
1 row in set (0.00 sec)

MySQL 127.0.0.1:15726+ SQL > \js
Switching to JavaScript mode...

MySQL 127.0.0.1:15726+ JS > util.importJson('/path/to/file/sample.json', {schema: 'test', table: 'example_table', tableColumn: 'json_data'});
Importing from file "/path/to/file/sample.json" to table `test`.`example_table` in MySQL Server at 127.0.0.1:15726

.. 1.. 1
Processed 204 bytes in 1 document in 0.0007 sec (1.36K documents/s)
Total successfully imported documents 1 (1.36K documents/s)

MySQL 127.0.0.1:15726+ JS > \sql
Switching to SQL mode... Commands end with ;

MySQL 127.0.0.1:15726+ SQL > SELECT `id`, `json_data`
                          -> FROM `test`.`example_table`\G
*************************** 1. row ***************************
       id: 1
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
1 row in set (0.0007 sec)
*************************** 2. row ***************************
       id: 2
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
2 rows in set (0.0006 sec)
0

There's no way to use LOAD DATA INFILE to load JSON, because as the comments say above, that statement only loads CSV data.

If you want to load a JSON document as a scalar to insert into one row of a table, you can use the LOAD_FILE() function.

INSERT INTO example_table SET json_data = LOAD_FILE('/home/path/json_data/sample.json');

This creates just one row to store the whole JSON document. It does not parse the JSON to insert separate rows for each entry in your sales_list array within the JSON.

Read the documentation I linked to for more information on the LOAD_FILE() function.

1
  • The MySQL shell (mysqlsh) has a parallel JSON/CSV/TSV bulk loader as of 8.0.17 (or so) Nov 12, 2019 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.