Use jq command to process json under linux

Official website document

Online tools

If you have not installed it, install it directly from the warehouse

###centos
yum install -y jq
###ubuntu
sudo apt-get install -y jq

If you yumdon’t have a warehouse, jqyou can followepel

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum repolist 

jqUsed in .represents the current object []represents the array. The following jsonis csdnjust to find an interface to grab it, and it is actually a one-line representation. .You can format the data directly through the object

{
    
    "redpackData":{
    
    "whiteList":[]},"barrageData":{
    
    "whiteList":[]},"barrageRedpackData":{
    
    "whiteList":[]},"background":{
    
    "default":"","dark":"https://g.csdnimg.cn/common/csdn-toolbar/images/toolbar_bg_dark.png","home":""},"loginBoxData":{
    
    "whiteList":["loc-toolbar.csdn.net","download.csdn.net","bbs.csdn.net"],"blackList":[],"ratio":0.5,"hours":4},"logo":[{
    
    "type":"default","title":"CSDN首页","link":"https://www.csdn.net/","qrcode":"https://img-home.csdnimg.cn/images/20201120101655.png","url":{
    
    "default":"https://img-home.csdnimg.cn/images/20201124032511.png","dark":"https://img-home.csdnimg.cn/images/20201124032506.png","home":"https://img-home.csdnimg.cn/images/20201124032511.png"}},{
    
    "type":"1111","title":"CSDN首页","link":"https://www.csdn.net/","qrcode":"https://img-home.csdnimg.cn/images/20201117050118.png","url":{
    
    "default":"https://img-home.csdnimg.cn/images/20201117015525.png","dark":"https://img-home.csdnimg.cn/images/20201117015528.png","home":"https://img-home.csdnimg.cn/images/20201117015525.png"},"time":{
    
    "start":"2020/11/03 00:00:00","end":"2020/11/13 23:59:59"}}],"menus":[{
    
    "id":"index","name":"首页","url":"https://www.csdn.net/","report":{
    
    "mod":"popu_336","dest":"https://www.csdn.net/","spm":"3001.5359"},"children":[]},{
    
    "id":"blog","name":"博客","url":"https://blog.csdn.net/","report":{
    
    "mod":"popu_336","dest":"https://blog.csdn.net/","spm":"3001.4477"},"icon":{
    
    "url":"https://img-home.csdnimg.cn/images/20201216020332.png","start":"2020/12/18 00:00:00","end":"2020/12/20 23:59:59","style":"position: absolute; vertical-align: middle; width: 16px; top: 3px; right: 3px;"},"children":[]},{
    
    "id":"edu","name":"程序员学院","url":"https://edu.csdn.net/","report":{
    
    "mod":"popu_336","dest":"https://edu.csdn.net/","spm":"3001.4482"},"children":[]},{
    
    "id":"download","name":"下载","url":"https://download.csdn.net/","report":{
    
    "mod":"popu_336","dest":"https://download.csdn.net/","spm":"3001.4483"},"children":[]},{
    
    "id":"bbs","name":"论坛","url":"https://bbs.csdn.net/","report":{
    
    "mod":"popu_336","dest":"https://bbs.csdn.net/","spm":"3001.4491"},"children":[]},{
    
    "id":"ask","name":"问答","url":"https://ask.csdn.net/","report":{
    
    "mod":"popu_336","dest":"https://ask.csdn.net/","spm":"3001.4492"},"children":[]},{
    
    "id":"code","name":"代码","url":"https://codechina.csdn.net/?utm_source=csdn_toolbar","report":{
    
    "mod":"popu_336","dest":"https://codechina.csdn.net/?utm_source=csdn_toolbar","spm":"3001.4493"},"children":[]},{
    
    "id":"live","name":"直播","url":"https://live.csdn.net/?utm_source=csdn_toolbar","report":{
    
    "mod":"popu_336","dest":"https://codechina.csdn.net/?utm_source=csdn_toolbar","spm":"3001.5342"},"children":[]},{
    
    "id":"book","name":"电子书","url":"https://book.csdn.net/","report":{
    
    "mod":"popu_336","dest":"https://book.csdn.net/","spm":"3001.4495"},"children":[]},{
    
    "id":"vip","name":"会员中心","url":"https://mall.csdn.net/vip","report":{
    
    "mod":"popu_336","dest":"https://mall.csdn.net/vip","spm":"3001.4496"},"icon":{
    
    "url":"https://img-home.csdnimg.cn/images/20201103110355.gif","start":"2020/11/03 00:00:00","end":"2020/11/13 23:59:59","style":"position: relative; vertical-align: middle; width: 14px; top: -1px; left: 0px;"},"children":[]}]}

At this time I want to see what it has one key, you can use the following command will be able to get directly to all of the first layer of keythe array.

jq keys

The output is as follows

[
  "background",
  "barrageData",
  "barrageRedpackData",
  "loginBoxData",
  "logo",
  "menus",
  "redpackData"
]

We can also directly get json through the network pipeline flow and come back for processing

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'|jq .

The output of the following data is a bit too much, so I took one of them{}

ubuntu@xxx:~$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0].committer'
{
    
    
  "login": "wtlangford",
  "id": 3422295,
  "node_id": "MDQ6VXNlcjM0MjIyOTU=",
  "avatar_url": "https://avatars.githubusercontent.com/u/3422295?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/wtlangford",
  "html_url": "https://github.com/wtlangford",
  "followers_url": "https://api.github.com/users/wtlangford/followers",
  "following_url": "https://api.github.com/users/wtlangford/following{/other_user}",
  "gists_url": "https://api.github.com/users/wtlangford/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/wtlangford/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/wtlangford/subscriptions",
  "organizations_url": "https://api.github.com/users/wtlangford/orgs",
  "repos_url": "https://api.github.com/users/wtlangford/repos",
  "events_url": "https://api.github.com/users/wtlangford/events{/privacy}",
  "received_events_url": "https://api.github.com/users/wtlangford/received_events",
  "type": "User",
  "site_admin": false
}

It also supports internal pipeline filtering and reorganization into Xinde json output
as follows

ubuntu@xxx:~$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
{
    
    
  "message": "Fix #2197 extended regex pattern example",
  "name": "William Langford"
}

Guess you like

Origin blog.csdn.net/a807719447/article/details/112977795