shell实现数据json格式转换

redis中按hash格式存放的数据,转换到json格式

例子1:

redis内数据格式
test1
"dbname=test1|username=db2inst3|passwd=db2inst3|ip=192.168.1.61|port=60004|" bmw "dbname=bmw|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|" test2 "dbname=test2|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
shell:
awk 'BEGIN{printf "["}' echo "hgetall SrcLogInfo " | ../wxBusCli | while read line do strA="|" #num=echo ${line | wc -c} awk 'BEGIN{printf "{"}' ret=\"$line\" echo ${ret} result=$(echo $line | grep "${strA}") if [ -n "$result" ]; then awk 'BEGIN{printf "},"}' else awk 'BEGIN{printf ":"}' fi done awk 'BEGIN{printf "]"}'
转换结果为:json格式
[{
        "test1": "dbname=test1|username=db2inst3|passwd=db2inst3|ip=192.168.1.61|port=60004|"
    },
    {
        "bmw": "dbname=bmw|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
    },
    {
        "test2": "dbname=test2|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
    }
]

例子2:

redis存的数据格式:
1) "db2inst1"
2) "{\"serverip\":\"192.168.1.113\",\"dbip\":\"192.168.1.225\",\"dbport\":\"50000\",\"nodename\":\"node225\",\"dbname\":\"db2_10\",\"dbaliname\":\"db2inst1\",\"instname\":\"db2inst1\"}"
3) "fea"
4) "{\"serverip\":\"192.168.1.113\",\"dbip\":\"192.168.1.225\",\"dbport\":\"50000\",\"nodename\":\"node225\",\"dbname\":\"db2_10\",\"dbaliname\":\"fea\",\"instname\":\"db2inst1\"}"
5) "bmw"
6) "{\"serverip\":\"192.168.1.113\",\"dbip\":\"192.168.1.61\",\"dbport\":\"60004\",\"nodename\":\"node61\",\"dbname\":\"test1\",\"dbaliname\":\"bmw\",\"instname\":\"db2inst3\"}"
shell:
awk 'BEGIN{printf "["}' echo "hgetall catainfo " | ../wxBusCli | while read line do i=$(($i+1)) if [[ $(($i%2)) -eq 0 ]];then printf $line, fi done awk 'BEGIN{printf "]"}'

//最后格式就不展示了

猜你喜欢

转载自www.cnblogs.com/kony9527/p/10534292.html