Use Shell scripts to convert MySQL table data to json format, mysqljson

Source: Internet
Author: User

Use Shell scripts to convert MySQL table data to json format, mysqljson

The script is as follows:

#!/bin/bashmysql -s -phello test >1.log <<EOFdesc t1;EOFlines="concat_ws(',',"count=`cat 1.log|wc -l`linenum=0while read linedo coloumname=`echo $line |awk '{print $1}'` let linenum=linenum+1  if [ $linenum -eq 1 ];then    lines=$lines"concat_ws(':','{"'"'$coloumname'"'"',if("$coloumname" is null or $coloumname='','null',concat('"'"'"',$coloumname,'"'"'"')))" elif [ $linenum -eq $count ];then    lines=$lines",concat_ws(':','"'"'$coloumname'"'"',if("$coloumname" is null or $coloumname='','null}',concat('"'"'"',$coloumname,'"'"'"}'))))" else    lines=$lines",concat_ws(':','"'"'$coloumname'"'"',if("$coloumname" is null or $coloumname='','null',concat('"'"'"',$coloumname,'"'"'"')))"  fidone < 1.logecho $lines

The data in table t1 is as follows:

mysql> select * from t1;+------+-------+| id   | name  |+------+-------+|    1 |       ||    1 | NULL  ||    2 | hello |+------+-------+3 rows in set (0.00 sec)

The script execution result is as follows:

concat_ws(',',concat_ws(':','{"id"',if(id is null or id='','null',concat('"',id,'"'))),concat_ws(':','"name"',if(name is null or name='','null}',concat('"',name,'"}'))))

The execution result in MySQL is as follows:

mysql> select concat_ws(',',concat_ws(':','{"id"',if(id is null or id='','null',concat('"',id,'"'))),concat_ws(':','"name"',if(name is null or name='','null}',concat('"',name,'"}')))) json_format from t1;+---------------------------+| json_format               |+---------------------------+| {"id":"1","name":null}    || {"id":"1","name":null}    || {"id":"2","name":"hello"} |+---------------------------+3 rows in set (0.00 sec)

 

Tip: differences between single quotes and double quotes in shell scripts

The single quotation marks and double quotation marks in shell scripts are character string delimiters rather than character delimiters.

Single quotesIt is used to keep the literal value of all characters in the quotation marks, even if the \ and carriage return in the quotation marks are no exception, but the single quotation marks cannot appear in the string. (Note that it is all, but the single quotation mark itself cannot appear in it.).

Double quotation marksIt is used to keep the nominal value of all characters in quotation marks (carriage return is no exception), except in the following cases:

  • $ You can add a variable name to get the value of the variable.

  • Reverse quotation marks still indicate command replacement

  • \ $ Indicates the nominal value of $

  • \ 'Indicates the literal value'

  • The nominal value of \ "represents ".

  • \ Represents the literal value \

  • In addition to the preceding conditions, the '\' before other characters has no special meaning and only indicates the nominal value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.