MySQL JSON Export from Table or Query result

Introduction

In this article we will learn how to use ZappyShell Command Line Tools for MySQL JSON Export from Table or Query result

JSON File format is becoming very popular due to its simplicity and smaller size. Most of NoSQL Databases are now using JSON for their document mechanism.

The problem

There is no native way of Exporting MySQL Data to JSON. You can use some coding techniques or libraries but when things become complex you will run out of options quickly.

Solution

ZappyShell for DB can export your MySQL data to JSON, PDF, Excel, CSV in one line command. This is just single exe less than 5 MB.. >Download here. Now Lets check how to do this.

Download and Install MySQL ODBC Driver

To export data from MySQL we will need ODBC Driver. In this tutorial we have use MySQL ODBC 5.2 Driver but if you are using different driver then change connection string accordingly.

Download and Install MySQL ODBC Driver from below URL
http://dev.mysql.com/downloads/connector/odbc/

Download MySQL 5.2 ODBC Driver

Download MySQL 5.2 ODBC Driver

MySQL JSON Export - Install MySQL ODBC Driver

MySQL JSON Export – Install MySQL ODBC Driver

Download and Install ZappyShell Command Line Tools

Now next step is we need to Download and Install ZappyShell Command Line tools from hereYou can Download MSI Installer or XCopy Zip file which you can simply extract and copy (see below).

ZappyShell Command Line Tools - Export JSON, Excel, CSV, PDF

ZappyShell Command Line Tools – Export JSON, Excel, CSV, PDF

MySQL JSON Export – Single table

c:\tools>db.exe export customers --out c:\data\customers..json --json --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL JSON Export – Multiple tables

In the following example we are exporting 3 tables (seperated by vertical bar)
c:\tools>db.exe export customers|orders|products --out c:\data\customers..json --json --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL JSON Export – Multiple tables (using pattern)

In below example all tables where name staring with cust will be exported from sales schema.
c:\tools>db.exe export cust% --schema Sales --out c:\data\customers..json --json --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL JSON Export – SQL Query

In the following example we are exporting output of SQL Query to JSON file. If you have complex query with multi line then you can use Map file with one dataset node.c:\tools>db.exe export "select * from customers where country like 'US%'" --out c:\data\customers.json --json --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

Using Map File for nested MySQL JSON Export

Lets say you want to export customer data along with orders they placed as nested JSON document then we have to supply Map file so JSON Generator can figure out how to JOIN two datasets and how to Shape JSON. In below example we are doing 3 nested levels for demo purpose. Customer joined with Orders by CustomerID key, Orders Joined with Employee by EmployeeID key.

MySampleMap.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings ver="1">
  <dataset id="cust" main="true">select * from Customers</dataset>
  <dataset id="ord">select * from Orders</dataset>
  <dataset id="emp">select * from Employees</dataset>
  <join parentid="cust" childid="ord" keys="CustomerID" />
  <join parentid="ord" childid="emp" keys="EmployeeID" />

  <map src="CustomerID" name="ID" />
  <map src="CompanyName" />
  <map name="Contact">
    <map src="Phone" />
    <map src="Fax" />
    <map name="Location">
      <map src="Country" />
      <map src="Region" />
    </map>
  </map>

  <map name="OrderIDList" dataset="ord" maptype="valuearray" src="OrderID"/>
  <map name="Orders" dataset="ord">
    <map src="OrderID" />
    <map src="OrderDate" />
    <map name="EmpInfo" dataset="emp">
      <map src="EmployeeID" />
      <map src="FirstName" />
    </map>
  </map>
</settings>
c:\tools>db.exe export --map c:\MySampleMap.xml --out c:\data\customers.json --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

customers.json : Output JSON generated from above Command

[
  {
    "ID": "ALFKI",
    "CompanyName": "Alfreds Futterkiste",
    "Contact": {
      "Phone": "030-0074321",
      "Fax": "030-0076545",
      "Location": {
        "Country": "Germany",
        "Region": null
      }
    },
    "OrderIDList": [10643,10692],
    "Orders": [
      {
        "OrderID": 10643,
        "OrderDate": "1997-08-25",
        "EmpInfo": [
          {
            "EmployeeID": 6,
            "FirstName": "Michael"
          }
        ]
      },
      {
        "OrderID": 10702,
        "OrderDate": "1997-10-13",
        "EmpInfo": [
          {
            "EmployeeID": 4,
            "FirstName": "Margaret"
          }
        ]
      }
    ]
  },
  {
    "ID": "ANATR",
    "CompanyName": "Ana Trujillo Emparedados y helados",
    "Contact": {
      "Phone": "(5) 555-4729",
      "Fax": "(5) 555-3745",
      "Location": {
        "Country": "Mexico",
        "Region": null
      }
    },
    "OrderIDList": [10308,10625,10759,10926],
    "Orders": [
      {
        "OrderID": 10308,
        "OrderDate": "1996-09-18",
        "EmpInfo": [
          {
            "EmployeeID": 7,
            "FirstName": "Robert"
          }
        ]
      },
      {
        "OrderID": 10625,
        "OrderDate": "1997-08-08",
        "EmpInfo": [
          {
            "EmployeeID": 3,
            "FirstName": "Janet"
          }
        ]
      }
    ]
  }
]

MySQL JSON Export – Split Files

Sometimes you may have need to split large amount of data into multiple files. With ZappyShell command line you can split data three different ways. Lets check each example.

MySQL JSON Export – Split By Row Count

In below example we are exporting MySQL customer table to JSON files and splitting files with maximum 1000 records per file option.

c:\tools>db.exe export customers --out c:\data\cust.json --split --splitrows 1000 --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL JSON Export – Split Files By File Size

In below example we are exporting MySQL customer table to JSON files and splitting files with maximum file size = 10MB option. You also use other size specifier like KB, GB, BYTES etc.

c:\tools>db.exe export customers --out c:\data\cust.json --split --splitsize 10MB --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL JSON Export – Split Files By Group By Column

In this example we have exported customers in to separate file by country.

c:\tools>db.exe export customers --out c:\data\cust.json --groupby Country --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
ZappyShell - Export MySQL Table to JSON, Split files by Country Column

ZappyShell – Export MySQL Table to JSON, Split files by Country Column

 

MySQL JSON Export – Compressed files

If you want to output compressed JSON (GZIP format) then use below command.

c:\tools>db.exe export customers --out c:\data\cust.json --gzip --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

Using date time place holders in JSON file name

If you want to use date time place holders in file name so it automatically uses specified date time parts use below command simply putting specifiers in file name anywhere.

c:\tools>db.exe export customers --out c:\data\cust_{{YYYY}}_{{MM}}_{{DD}}_{{HH24}}_{{MI}}_{{SS}}.json --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL JSON Export – Multi Content format

To export data in Multi-Content JSON format use below command. Multi content format append each record as JSON document (no opening closing array brackets)
c:\tools>db.exe export customers --out c:\data\cust.json --json-multi-content --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

Conclusion

In this article we have gone through few ways to use ZappyShell Command Line Tools to export data from MySQL to JSON. You can also use same command line to export in CSV, Excel, PDF format.

Reference Links

Posted in MySQL and tagged , , , .

Leave a Reply