用python方便的格式化json

用python方便的格式化json

用python方便的格式化json

用python来格式化json输出

最近在研究F5的 rest api, 通过curl 来get输出的都是json。问题也跟着来了,默认输出是不带格式的,人类很难读。
同样redis也是这样,用redis-cli输出的一坨子json很难识别。

比如这样:

$ curl -k -u admin:admin -X GET https://1xxx/mgmt/tm/ltm/pool/poolaaa/members/stats/

{"kind":"tm:ltm:pool:members:memberscollectionstats","selfLink":"https://localhost/mgmt/tm/ltm/pool/poolaaa/members/stats?ver=1x.x.x","entries":{"https://localhost/mgmt/tm/ltm/pool/poolaaa/members/~Common~1xxxx2
.....
省略输出了

万能的python登场

只需要用我们万能的python,就可以帮助我们格式化成人类习惯的格式了:

python -m json.tool

$curl -k -u admin:admin -X GET https://xxx/mgmt/tm/ltm/pool/poolaaa/members/stats/ | python -m json.tool

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2626 100 2626 0 0 3999 0 --:--:-- --:--:-- --:--:-- 3996
{
"entries": {
"https://localhost/mgmt/tm/ltm/pool/pool_aaa/members/~Common~xxxx:xxx0/stats": {
"nestedStats": {
"entries": {
"addr": {
"description": "1xxx"
},
"connq.ageEdm": {
"value": 0
},
。。。。
....

同样的道理.redis-cli也可以

比如,我要看ansible收集的fact

./redis-cli --raw -p xxxx get "ansible_factxxxx" | python -m json.tool

{
"ansible_all_ipv4_addresses": [
"xxxx"
],
"ansible_all_ipv6_addresses": [
"xxxx"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/09/2012",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-229.el7.x86_64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "rhel/root",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/rhel-root"
},

.......

python在手,工作无忧啊。

完.