convert mysql query output into json/yaml array
Project description
convert mysql query output (in table format) to json or yaml
Summary
We will take as input mysql query output, then convert that to an array of the records in yaml or json format, using the ‘header’ line from the query as the tags for each of the fields.
Usage
usage: tbltojson.py [-h] [-p] [-t {json,yaml}] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-s] [file [file ...]] table (mysql query output) to json conversion positional arguments: file optional arguments: -h, --help show this help message and exit -p, --pretty -t {json,yaml}, --type {json,yaml} Output type, json or yaml -l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --loglevel {DEBUG,INFO,WARNING,ERROR,CRITICAL} Log level (DEBUG,INFO,WARNING,ERROR,CRITICAL) default is: WARNING -s, --save save select command line arguments (default is always) in "~/.tbltojson.conf" file
Arguments
–pretty, make the output (json) pretty. yaml is already pretty. default is false.
–help, the usage message is printed.
–type, json or yaml (this is the OUTPUT type, input can be either), default yaml.
—loglevel, for debugging, default INFO.
–save, save current arguments to persistent file in home directory, this file will be read as if it came from the command line in subsequent invocations of this program. To remove it you have to remove the ~/.tbltojson.conf file manually. Do this for making pretty default, for example. the default is no save is done.
file, the file to convert. stdin used if file not specified.
Examples
A lot of the openstack output comes in mysql format, not too useful in scripts.
The file mysqlqueryoutput.txt:
+--------------------------------------+----------+--------------------------------------+-------------+-----------------+-------------+ | UUID | Name | Instance UUID | Power State | Provision State | Maintenance | +--------------------------------------+----------+--------------------------------------+-------------+-----------------+-------------+ | ba8fa1f3-3c28-4dd2-999c-34e65f90f8c1 | compute7 | c547c1d2-3a63-49bb-8e57-18255cb0e8a8 | power on | active | False | | 00a9a1c7-3fcc-49f4-b9b5-6a3c96db0a3b | control1 | 11fc9dd3-fd27-43d6-9ef4-87788075db07 | power on | active | False | | 6599588c-ef48-4a6d-8c0c-8a7075cc26fd | control2 | 8f3bc8d3-fb02-43c9-990a-6c81b40132a1 | power on | active | False | | 01fe66f7-709e-4ba1-8c2c-2ae6d6819b0f | control3 | 726550e4-d9bd-4bdc-8737-baf794b6810d | power on | active | False | | 1e3d2c6f-c433-4c6f-9e9e-43a6d9acc8cc | None | None | power off | manageable | False | | 10f0eb95-0d30-4dfb-913c-b061e0efe6e3 | None | None | power off | manageable | False | | 2809746e-53c1-407b-985c-59f6e69c6baf | None | None | power off | manageable | False | | c1209ff0-3570-486f-bf9f-ce8dd1576d42 | None | None | power off | manageable | False | | c9931a69-2883-4430-9cbb-54bf621c4d31 | None | None | power off | manageable | False | | 85cc5719-164d-4d12-a0ea-6cab1392a244 | None | None | power off | manageable | False | +--------------------------------------+----------+--------------------------------------+-------------+-----------------+-------------+
These examples convert the tbl output to json or yaml:
tbltojson mysqlqueryoutput.txt -t json [{"Instance_UUID": "c547c1d2-3a63-49bb-8e57-18255cb0e8a8", "Maintenance": "False", "UUID": "ba8fa1f3-3c28-4dd2-999c-34e65f90f8c1", "Power_State": "power on", "Provision_State": "active", "Name": "compute7"}, {"Instance_UUID": "11fc9dd3-fd27-43d6-9ef4-87788075db07", "Maintenance": "False", "UUID": "00a9a1c7-3fcc-49f4-b9b5-6a3c96db0a3b", "Power_State": "power on", "Provision_State": "active", "Name": "control1"}, {"Instance_UUID": "8f3bc8d3-fb02-43c9-990a-6c81b40132a1", "Maintenance": "False", "UUID": "6599588c-ef48-4a6d-8c0c-8a7075cc26fd", "Power_State": "power on", "Provision_State": "active", "Name": "control2"}, {"Instance_UUID": "726550e4-d9bd-4bdc-8737-baf794b6810d", "Maintenance": "False", "UUID": "01fe66f7-709e-4ba1-8c2c-2ae6d6819b0f", "Power_State": "power on", "Provision_State": "active", "Name": "control3"}, {"Instance_UUID": "None", "Maintenance": "False", "UUID": "1e3d2c6f-c433-4c6f-9e9e-43a6d9acc8cc", "Power_State": "power off", "Provision_State": "manageable", "Name": "None"}, {"Instance_UUID": "None", "Maintenance": "False", "UUID": "10f0eb95-0d30-4dfb-913c-b061e0efe6e3", "Power_State": "power off", "Provision_State": "manageable", "Name": "None"}, {"Instance_UUID": "None", "Maintenance": "False", "UUID": "2809746e-53c1-407b-985c-59f6e69c6baf", "Power_State": "power off", "Provision_State": "manageable", "Name": "None"}, {"Instance_UUID": "None", "Maintenance": "False", "UUID": "c1209ff0-3570-486f-bf9f-ce8dd1576d42", "Power_State": "power off", "Provision_State": "manageable", "Name": "None"}, {"Instance_UUID": "None", "Maintenance": "False", "UUID": "c9931a69-2883-4430-9cbb-54bf621c4d31", "Power_State": "power off", "Provision_State": "manageable", "Name": "None"}, {"Instance_UUID": "None", "Maintenance": "False", "UUID": "85cc5719-164d-4d12-a0ea-6cab1392a244", "Power_State": "power off", "Provision_State": "manageable", "Name": "None"}]
tbltojson mysqlqueryoutput.txt -t json -p [ { "Instance_UUID": "c547c1d2-3a63-49bb-8e57-18255cb0e8a8", "Maintenance": "False", "Name": "compute7", "Power_State": "power on", "Provision_State": "active", "UUID": "ba8fa1f3-3c28-4dd2-999c-34e65f90f8c1" }, { "Instance_UUID": "11fc9dd3-fd27-43d6-9ef4-87788075db07", "Maintenance": "False", "Name": "control1", "Power_State": "power on", "Provision_State": "active", "UUID": "00a9a1c7-3fcc-49f4-b9b5-6a3c96db0a3b" }, ...
tbltojson mysqlqueryoutput.txt -t yaml - {Instance_UUID: c547c1d2-3a63-49bb-8e57-18255cb0e8a8, Maintenance: 'False', Name: compute7, Power_State: power on, Provision_State: active, UUID: ba8fa1f3-3c28-4dd2-999c-34e65f90f8c1} - {Instance_UUID: 11fc9dd3-fd27-43d6-9ef4-87788075db07, Maintenance: 'False', Name: control1, Power_State: power on, Provision_State: active, UUID: 00a9a1c7-3fcc-49f4-b9b5-6a3c96db0a3b} - {Instance_UUID: 8f3bc8d3-fb02-43c9-990a-6c81b40132a1, Maintenance: 'False', Name: control2, Power_State: power on, Provision_State: active, UUID: 6599588c-ef48-4a6d-8c0c-8a7075cc26fd} - {Instance_UUID: 726550e4-d9bd-4bdc-8737-baf794b6810d, Maintenance: 'False', Name: control3, Power_State: power on, Provision_State: active, UUID: 01fe66f7-709e-4ba1-8c2c-2ae6d6819b0f} - {Instance_UUID: None, Maintenance: 'False', Name: None, Power_State: power off, Provision_State: manageable, UUID: 1e3d2c6f-c433-4c6f-9e9e-43a6d9acc8cc} - {Instance_UUID: None, Maintenance: 'False', Name: None, Power_State: power off, Provision_State: manageable, UUID: 10f0eb95-0d30-4dfb-913c-b061e0efe6e3} - {Instance_UUID: None, Maintenance: 'False', Name: None, Power_State: power off, Provision_State: manageable, UUID: 2809746e-53c1-407b-985c-59f6e69c6baf} - {Instance_UUID: None, Maintenance: 'False', Name: None, Power_State: power off, Provision_State: manageable, UUID: c1209ff0-3570-486f-bf9f-ce8dd1576d42} - {Instance_UUID: None, Maintenance: 'False', Name: None, Power_State: power off, Provision_State: manageable, UUID: c9931a69-2883-4430-9cbb-54bf621c4d31} - {Instance_UUID: None, Maintenance: 'False', Name: None, Power_State: power off, Provision_State: manageable, UUID: 85cc5719-164d-4d12-a0ea-6cab1392a244}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.