Linux命令行(Console)解析JSON

有些Linux服务API返回的数据为JSON数据,但是直接在命令行看,看起来很不方便,都拧巴在一起了,比如这样。

[root@songshucang nginx]# curl http://localhost:8500/v1/catalog/service/xxx
[{"ID":"5c82060b-caa6-082c-27ac-4e64d38d35f1","Node":"izwz99ridy7gp33ry7hmfqz","Address":"172.18.177.227","Datacenter":"default","TaggedAddresses":{"lan":"172.18.177.227","wan":"172.18.177.227"},"NodeMeta":{"consul-network-segment":""},"ServiceID":"application-ecs-8090","ServiceName":"songshu-cang","ServiceTags":["songshu"],"ServiceAddress":"172.18.177.227","ServicePort":8090,"ServiceEnableTagOverride":false,"CreateIndex":502,"ModifyIndex":502},{"ID":"5c82060b-caa6-082c-27ac-4e64d38d35f1","Node":"izwz99ridy7gp33ry7hmfqz","Address":"172.18.177.227","Datacenter":"default","TaggedAddresses":{"lan":"172.18.177.227","wan":"172.18.177.227"},"NodeMeta":{"consul-network-segment":""},"ServiceID":"application-ecs-8091","ServiceName":"songshu-cang","ServiceTags":["songshu"],"ServiceAddress":"172.18.177.227","ServicePort":8091,"ServiceEnableTagOverride":false,"CreateIndex":520,"ModifyIndex":520}][root@songshucang nginx]#

不过Linux内置了一些命令,比如jq

[root@songshucang nginx]# yum info jq
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name        : jq
Arch        : x86_64
Version     : 1.5
Release     : 1.el7
Size        : 351 k
Repo        : installed
From repo   : epel
Summary     : Command-line JSON processor
URL         : http://stedolan.github.io/jq/
License     : MIT and ASL 2.0 and CC-BY and GPLv3
Description : lightweight and flexible command-line JSON processor
            :
            :  jq is like sed for JSON data – you can use it to slice
            :  and filter and map and transform structured data with
            :  the same ease that sed, awk, grep and friends let you
            :  play with text.
            :
            :  It is written in portable C, and it has zero runtime
            :  dependencies.
            :
            :  jq can mangle the data format that you have into the
            :  one that you want with very little effort, and the
            :  program to do so is often shorter and simpler than
            :  you'd expect.

安装与使用jq

sudo yum install jq -y
使用jq

总结

简单的介绍了一个Linux的命令。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • Linux中在命令行中使用JQ来解析修改JSON字符串 在现实开发场景中, 经常需要在命令行中做一些解析json数...
    橘汁绊饭阅读 14,398评论 2 7
  • 风云两甲子 弦歌三世纪 美哉吾校 饮水思源 为世界之光
    诚哉如斯阅读 350评论 0 1
  • 读第一章 儿童是什么 成人对儿童成长规律的漠视和无知,导致成人对儿童早期教育过程中进行强制干预,给儿童成长带来限...
    湘其阅读 1,104评论 0 0
  • 一杯奶盖,漫溢出期待感的淡绿香味,对即将确定到来的另一种未知生活。 送别无言,再见蓦然。 直到现在,也没有你将要离...
    银色脏罐子阅读 297评论 0 0