How to select mysql from bash (shell) and output it in JSON

Asked 2 years ago, Updated 2 years ago, 127 views

I would like to know how to select mysql on bash and output it on JSON.

Specifically,

 echo "SELECT* from testtable;" | mysql --defaults-extra-file=./my.conf testdb

I would like to print the results in JSON and use the jq command to handle the values.

The version of mysql is

$mysql --version
mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper

Please let me know if you know.

mysql bash jq

2022-09-30 19:13

1 Answers

Run pipe install csvkit to install the csvjson command.Then,

 mysql-B-e "select * from some_table" some_db | csvjson-d$'\t'

If you run , you will be able to do what you want to do what you want to do.

If there is only one thing that doesn't work and the table contains tab characters, you need to think about it a little bit more.


2022-09-30 19:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.