jq json格式化工具

今天在看mysql 8.0特性的  set persist 時候,看到一個博客對mysqld-auto.cnf 的json格式化,用的工具是jq,當時以爲是自己寫的小工具,就去網上查了一下,還真發現有人寫了這麼一個小工具,還是挺好用的,就把文章轉載過來,以後還能用。

[root@anedbtest01 ~]# cat  /u01/data/ane20000/mysqld-auto.cnf|/tmp/jq
{
  "Version": 1,
  "mysql_server": {
    "max_connections": {
      "Value": "500",
      "Metadata": {
        "Timestamp": 1558056501802792,
        "User": "root",
        "Host": "localhost"
      }
    }
  }
}
 

 

 

作者:婁超

在web 2.0時代json這種直觀、靈活、高效數據格式基本已經成爲一種標準格式,從各種web api,到配置文件,甚至現在連mysql都開始支持json作爲數據類型。

但是在平時開發運維中往往因爲格式問題或者輸出數據太多,體驗不是很爽,之前我就經常需要藉助一些json自動語法檢查、格式化、分層摺疊的工具(如http://www.bejson.com/ ), 往往還是需要來回拷貝,還是覺得很麻煩。

所以,一直希望有個linux命令行的簡單命令(python的json.tool模塊只能格式化顯示),偶然發現了這個jq的小工具,感覺很強大,就分享一下。

 

不說廢話了,直接例子說明吧

 

使用說明

1、格式化json數據

echo '{"kind": "Service", "apiVersion": "v1", "status": {"loadBalancer": true}}'|jq .{  "kind": "Service",  "apiVersion": "v1",  "status": {    "loadBalancer": true
  }}

只要3個字母搞定,其中jq是工具命令,後面參數是過濾選擇參數,"." 表示直接輸出完整的json數據。

usage: jq [options]  [file...]

 

2. 過濾出需要的字段信息

cat service.json {    "kind": "Service",    "apiVersion": "v1",    "metadata": {        "name": "kubernetes",        "namespace": "default",     },    "spec": {        "ports": [             {                "protocol": "TCP",                "port": 443,                "targetPort": 443,                "nodePort": 0             }         ], .....很多數據 } cat service.json|jq .metadata.name"kubernetes"

3、過濾出某個數組對象

cat service.json|jq .spec.ports[0]  
{  "protocol": "TCP",  "port": 443,  "targetPort": 443,  "nodePort": 0}

 

4、使用管道過濾並調整輸出

cat service.json|jq  ".spec.ports[0]| {srcPort: .port, targetPort: .targetPort}"    {  "srcPort": 443,  "targetPort": 443}

如果數據的下標爲不填,將輸出所有數組的過濾值,如 cat service.json|jq ".spec.ports[]| {srcPort: .port, targetPort: .targetPort}"

 

5、 自定義輸出數組

cat t.json
{  "parents": [
    {      "sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7",      "url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7",      "html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7"
    },
    {      "sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a",      "url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a",      "html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a"
    }
  ]
}
cat t.json|jq ' { html_urls: [.parents[].html_url]}'{  "html_urls": [    "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7",    "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a"
  ]
}

 

6、支持條件查詢

舉個簡單例子,只輸出tcp協議端口信息

cat service.json|jq .spec.ports[0]  
{  "protocol": "TCP",  "port": 443,  "targetPort": 443,  "nodePort": 0}cat service.json |jq 'if .spec.ports[0].protocol = "tcp" then .spec.ports[0] else "not tcp" end'

注意jq 的語法有點奇怪, 必須 if else 同時存在,數字相等是用 "==",字符串是"="

總之,jq 功能是很強大的,它是一個c語言寫的小工具,包括很多正則匹配,更多高級使用方法,見 https://stedolan.github.io/jq/manual/

 

安裝說明

Debian and Ubuntu 下



作者:yijian2595
鏈接:https://www.jianshu.com/p/f0713384ff02
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章