在PostgreSQL命令行psql里格式化输出json字段
生活随笔
收集整理的這篇文章主要介紹了
在PostgreSQL命令行psql里格式化输出json字段
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
在pgsql的psql命令里直接select輸出json字段是一長(zhǎng)串字符,這對(duì)閱讀非常不友好,查了好久也沒查到pgsql有格式化輸出json字段的函數(shù),只好折騰一番。
表結(jié)構(gòu)如下:
SELECT的格式化輸出:
pgsql=#?select?id,replace(replace(regexp_replace(info::text?,?',?"'?,?concat(',',chr(10),chr(9),'"'),'g')?,'{'?,?concat('{',chr(10),chr(9)))?,'}'?,?concat(chr(10),'}'))?as?info?from?test;id?|?????????????????????????????info?????????????????????????????? ----+---------------------------------------------------------------1?|?{????????????????????????????????????????????????????????????+|?????????"a":?[1,?2,?3],??????????????????????????????????????+|?????????"b":?[4,?5,?6]???????????????????????????????????????+|?}2?|?{????????????????????????????????????????????????????????????+|?????????"a":?[7,?8,?9],??????????????????????????????????????+|?????????"b":?[10,?11,?12]????????????????????????????????????+|?}3?|?{????????????????????????????????????????????????????????????+|?????????"guid":?"9c36adc1-7fb5-4d5b-83b4-90356a46061a",??????+|?????????"name":?"Angela?Barton",?????????????????????????????+|?????????"tags":?["enim",?????????????????????????????????????+|?????????"aliquip",???????????????????????????????????????????+|?????????"qui"],??????????????????????????????????????????????+|?????????"address":?"178?Howard?Place,?Gulf,?Washington,?702",+|?????????"company":?"Magnafone",??????????????????????????????+|?????????"latitude":?19.793713,???????????????????????????????+|?????????"is_active":?true,???????????????????????????????????+|?????????"longitude":?86.513373,??????????????????????????????+|?????????"registered":?"2009-11-07T08:53:22?+08:00"???????????+|?} (3?行記錄為了方便今后每次調(diào)用,寫成函數(shù):
create?function?format_json(text)?returns?text?as?$$ select?replace(replace(regexp_replace($1?,?',?"'?,?concat(',',chr(10),chr(9),'"'),'g'), '{'?,?concat('{',chr(10),chr(9)))?,'}'?,?concat(chr(10),'}'))?as?json_s; $$?language?sql;這樣看起來就簡(jiǎn)潔直觀多了:
pgsql=#?select?id,format_json(info::text)?as?info?from?test?where?id=3;id?|?????????????????????????????info?????????????????????????????? ----+---------------------------------------------------------------3?|?{????????????????????????????????????????????????????????????+|?????????"guid":?"9c36adc1-7fb5-4d5b-83b4-90356a46061a",??????+|?????????"name":?"Angela?Barton",?????????????????????????????+|?????????"tags":?["enim",?????????????????????????????????????+|?????????"aliquip",???????????????????????????????????????????+|?????????"qui"],??????????????????????????????????????????????+|?????????"address":?"178?Howard?Place,?Gulf,?Washington,?702",+|?????????"company":?"Magnafone",??????????????????????????????+|?????????"latitude":?19.793713,???????????????????????????????+|?????????"is_active":?true,???????????????????????????????????+|?????????"longitude":?86.513373,??????????????????????????????+|?????????"registered":?"2009-11-07T08:53:22?+08:00"???????????+|?} (1?行記錄)轉(zhuǎn)載于:https://my.oschina.net/cytan/blog/542100
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的在PostgreSQL命令行psql里格式化输出json字段的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5 canvas绘制圆形进度实例
- 下一篇: python相关概念