前言
環(huán)境:centos7.9
在有外網(wǎng)的情況下,如果對數(shù)據(jù)沒有嚴(yán)格的保密,那么可以通過在線json解析網(wǎng)站https://www.sojson.com/
對json字符串進(jìn)行格式化。本篇講如何在Linux環(huán)境下使用命令對json字符串進(jìn)行格式化。
python的json.tool工具
#先創(chuàng)建一個json格式的文件,如下
#json字符串都是一行,可讀性差,需要格式化
[root@master ~]# cat python.py
{"name": "中國","province": [{"name": "黑龍江","cities": {"city": ["哈爾濱", "大慶"]}}, {"name": "廣東","cities": {"city": ["廣州", "深圳", "珠海"]}}, {"name": "臺灣","cities": {"city": ["臺北", "高雄"]}}, {"name": "新疆","cities": {"city": ["烏魯木齊"]}}]}
#使用python 的json.tool模塊工具對json字符串格式化
#雖然能正常格式化,但中文亂碼了
[root@master ~]# python -m json.tool python.py #與下面這條命令等價
[root@master ~]# cat python.py | python -m json.tool #與上面這條命令等價
{
"name": "\u4e2d\u56fd",
"province": [
{
"cities": {
"city": [
"\u54c8\u5c14\u6ee8", #中文亂碼了
"\u5927\u5e86"
]
},
"name": "\u9ed1\u9f99\u6c5f"
},
{
"cities": {
"city": [
"\u5e7f\u5dde",
"\u6df1\u5733",
"\u73e0\u6d77"
]
},
"name": "\u5e7f\u4e1c"
},
{
"cities": {
"city": [
"\u53f0\u5317",
"\u9ad8\u96c4"
]
},
"name": "\u53f0\u6e7e"
},
{
"cities": {
"city": [
"\u4e4c\u9c81\u6728\u9f50"
]
},
"name": "\u65b0\u7586"
}
]
}
[root@master ~]#
解決json.tool中文亂碼
修改json.tool程序,該程序存在于python系統(tǒng)庫安裝路徑下的json/tool.py,
json.dump方法中增加參數(shù)ensure_ascii=False,即讓json.tool程序不強(qiáng)行保證json的內(nèi)容都轉(zhuǎn)義為ascii編碼,中文原樣輸出即可。文章來源:http://www.zghlxwxcb.cn/news/detail-553612.html
[root@master ~]# find / -name tool.py
/usr/lib64/python2.7/json/tool.py
[root@master ~]# vim /usr/lib64/python2.7/json/tool.py #修改tool.py文件,修改完成保存退出即可
31 except ValueError, e:
32 raise SystemExit(e)
33 with outfile:
34 json.dump(obj, outfile, sort_keys=True,
35 indent=4,ensure_ascii=False,separators=(',', ': ')) #添加ensure_ascii=False
36 outfile.write('\n')
[root@master ~]# cat python.py | python -m json.tool #驗(yàn)證,中文正常
{
"name": "中國",
"province": [
{
"cities": {
"city": [
"哈爾濱",
"大慶"
]
},
"name": "黑龍江"
},
{
"cities": {
"city": [
"廣州",
"深圳",
"珠海"
]
},
"name": "廣東"
},
{
"cities": {
"city": [
"臺北",
"高雄"
]
},
"name": "臺灣"
},
{
"cities": {
"city": [
"烏魯木齊"
]
},
"name": "新疆"
}
]
}
jq工具
jq工具也是一個很好用的json格式化命令行工具。jq包默認(rèn)不存在centos鏡像倉庫中,所以需要安裝epel源。文章來源地址http://www.zghlxwxcb.cn/news/detail-553612.html
[root@master ~]# yum -y install epel-release
[root@master ~]# yum install jq -y
[root@master ~]# jq . python.py #與下面命令等價,有個點(diǎn).號
[root@master ~]# cat python.py | jq . #與上面命令等價,有個點(diǎn).號
{
"name": "中國",
"province": [
{
"name": "黑龍江",
"cities": {
"city": [
"哈爾濱",
"大慶"
]
}
},
{
"name": "廣東",
"cities": {
"city": [
"廣州",
"深圳",
"珠海"
]
}
},
{
"name": "臺灣",
"cities": {
"city": [
"臺北",
"高雄"
]
}
},
{
"name": "新疆",
"cities": {
"city": [
"烏魯木齊"
]
}
}
]
}
[root@master ~]#
jq命令語法
Usage: jq [options] <jq filter> [file...]
jq [options] --args <jq filter> [strings...]
jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
jq是一個處理JSON字符串的工具,將給定的JSON字符串輸入經(jīng)過過濾器過濾后并以JSON形式生成輸出結(jié)果
最簡單的過濾器是點(diǎn).,它將jq的輸入復(fù)制到它的輸出且未經(jīng)修改(除了格式)。
常用options:
-c 緊湊輸出為一行,未格式化;
-n use `null` as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
--rawfile a f set variable $a to a string consisting of the contents of <f>;
--args remaining arguments are string arguments, not files;
--jsonargs remaining arguments are JSON arguments, not files;
-- terminates argument processing;
總結(jié)
1、Python命令對json字符串進(jìn)行格式化
python -m json.tool python.py #與下面這條命令等價
cat python.py | python -m json.tool #與上面這條命令等價
2、jq命令對json字符串進(jìn)行格式化
yum -y install epel-release #要安裝epel源
yum install jq -y #安裝jq命令
jq . python.py #與下面命令等價,有個點(diǎn).號
cat python.py | jq . #與上面命令等價,有個點(diǎn).號
到了這里,關(guān)于linux下json字符串格式化、解決json.tool中文亂碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!