Skip to content

Instantly share code, notes, and snippets.

@michabbb
Last active January 15, 2018 15:29
Show Gist options
  • Save michabbb/62d20416b92e3555c732ff13776ec70d to your computer and use it in GitHub Desktop.
Save michabbb/62d20416b92e3555c732ff13776ec70d to your computer and use it in GitHub Desktop.
Mysql - Create json date from table
SELECT
CONCAT(',\',"', c.COLUMN_NAME, '":\',', '\'"\', IFNULL(','REPLACE(TRIM(',c.COLUMN_NAME, '),\'"\',\'\\\\"\'), \'null\'),', '\'"\'')
FROM information_schema.COLUMNS c
WHERE c.TABLE_NAME = 'xxxxx';
SELECT
CONCAT('{'
/* PASTE RESULT HERE AND FIX SOME MINOR COMMA STUFF */
'}')
FROM xxxx a;
@michabbb
Copy link
Author

because the mysql bash result adds an extra \ i use sed to replace it to get a valid json
cat sql|mysql -N your_database|sed 's/\\"/\"/g' > result.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment