MySQL Shell 9.1  /  ...  /  Importing JSON Documents With the Mysqlsh Command Interface

11.2.2 Importing JSON Documents With the Mysqlsh Command Interface

With the mysqlsh command interface, you invoke the JSON import utility as follows:

mysqlsh user@host:port/mydb -- util importJson <path> [options]
or
mysqlsh user@host:port/mydb -- util import-json <path> [options]

For information on this syntax, see Section 5.8, “API Command Line Integration”. For the JSON import utility, specify the parameters as follows:

user

The user name for the user account that is used to run the JSON import utility.

host

The host name for the MySQL server.

port

The port number for MySQL Shell's connection to the MySQL server. The default port for this connection is 33060.

mydb

The name of the target database. When invoking the JSON import utility from the command line, you must specify the target database. You can either specify it in the URI-like connection string, or using an additional --schema command line option.

path

The file path for the file (or FIFO special file) containing the JSON documents to be imported.

options

The --collection, --table, and --tableColumn options specify a target collection or a target table and column. The relationships and defaults when the JSON import utility is invoked using the mysqlsh command interface are the same as when the corresponding options are used in a MySQL Shell session. If you specify none of these options, the utility defaults to using or creating a target collection with the name of the supplied import file (without the file extension).

The --convertBsonTypes option converts BSON data types that are represented using extensions to the JSON format. The additional control options for specific BSON data types can also be specified; for a list of these control options and the default type conversions, see Section 11.2.4, “Conversions for Representations of BSON Data Types”. The --convertBsonOid option is automatically set on when you specify --convertBsonTypes. When importing data from MongoDB, --convertBsonOid must be specified if you do not convert the BSON types, because MySQL Server requires the _id value to be converted to the varbinary(32) type. --extractOidTime=field_name can be used to extract the timestamp from the _id value into a separate field.

The following example imports the JSON documents in the file products.json to the products collection in the mydb database:

mysqlsh user@localhost/mydb -- util importJson products.json --collection=products