linuxのjson神器のjq

14643 ワード

macインストール:
brew install jq
centosインストール:
yum install jq
uuntu:インストール:
ap-get install jq
jq            JSON    
    、  、    
JSON(JavaScript Object Notation)              。
  javascript(Standard ECMA-262 3rd Edition - December 1999)      
JSON         :   /  。 
JSON       

“  / ”    ,       ,         (object),  (record),  (struct),  (dictionary),   (hash table),   (keyed list)  .
                  (array)

JSON       

     、    。       ”{” ,   ”}”。          ”:”  。      ”,”  。
       ,  ,   ,  ,  ,null。 
  :       
   :      
   :true false 
  :      
  :      
null
jqコマンドのフォーマット
jq [options] filter [files]
**options:**
--version:  jq        
--slurp/-s:            。
--raw-input/-RJSON  ,                 。
--null-input/ -n:       ,       null    。        JSON--compact-output /-c:     ,       JSON--colour-output / -C--monochrome-output / -M
--ascii-output /-a:       ASCII

-raw-output /-r :             ,          (        )
filter:
.   :     
.foo:       ,foo    。
.[foo] :        。foo      。
.[]:           
, :             ,     
| :                     
files:
例としてJOSNファイルを使用します.
[{"hostCompany":"Beijing Autelan Technology","hostModel":"CS-VIC-2000-C","hostsn":"01010730b12014A00477","mac":"00:1F:64:CE:F3:8E","cpuModel":"MIPS 74Kc V4.12","cpuSN":"000000","memoryModel":"abcdefg","memorySN":"000000","boardSN":"01010730b12014A00477","networkCardMac":"00:1F:64:CE:F3:8F","lowFreModel":"AR9344","lowFreSN":"000000","hignFreModel":"AR9582","hignFreSN":"000000","gpsModel":"abcdefg","gpsSN":"000000","MEID_3g":"A000004E123ABD2","Company_3g":"ZTEMT INCORPORATED","modelOf3g":"MC271X","snOf3g":"A000004E123ABD2","iccid":"89860314400200885980","Operators":"CTCC","hardVersion":"1.20","firmwareVersion":"1.0.6.29"}]
例:
$ jq . apinfo.json   
[
  {
    "firmwareVersion": "1.0.6.29",
    "hardVersion": "1.20",
    "Operators": "CTCC",
    "iccid": "DATA DATA",
    "snOf3g": "A000004E123ABD2",
    "modelOf3g": "MC271X",
    "Company_3g": "ZTEMT INCORPORATED",
    "MEID_3g": "A000004E123ABD2",
    "memorySN": "000000",
    "memoryModel": "abcdefg",
    "cpuSN": "000000",
    "cpuModel": "MIPS 74Kc V4.12",
    "mac": "00:1F:64:CE:F3:8E",
    "hostsn": "01010730b12014A00477",
    "hostModel": "CS-VIC-1999-C",
    "stCompany": "Beijing Autelan Technology",
    "boardSN": "01010730b12014A00477",
    "networkCardMac": "00:1F:64:CE:F3:8F",
    "lowFreModel": "AR9344",
    "lowFreSN": "000000",
    "hignFreModel": "AR9582",
    "hignFreSN": "000000",
    "gpsModel": "abcdefg",
    "gpsSN": "000000"
  }
]

jq '.[]' apinfo.json 
{
  "firmwareVersion": "1.0.6.29",
  "hardVersion": "1.20",
  "Operators": "CTCC",
  "iccid": "DATA DATA",
  "snOf3g": "A000004E123ABD2",
  "modelOf3g": "MC271X",
  "Company_3g": "ZTEMT INCORPORATED",
  "MEID_3g": "A000004E123ABD2",
  "memorySN": "000000",
  "memoryModel": "abcdefg",
  "cpuSN": "000000",
  "cpuModel": "MIPS 74Kc V4.12",
  "mac": "00:1F:64:CE:F3:8E",
  "hostsn": "01010730b12014A00477",
  "hostModel": "CS-VIC-1999-C",
  "stCompany": "Beijing Autelan Technology",
  "boardSN": "01010730b12014A00477",
  "networkCardMac": "00:1F:64:CE:F3:8F",
  "lowFreModel": "AR9344",
  "lowFreSN": "000000",
  "hignFreModel": "AR9582",
  "hignFreSN": "000000",
  "gpsModel": "abcdefg",
  "gpsSN": "000000"
}

$ jq -r .[].mac apinfo.json 
00:1F:64:CE:F3:8E

$ jq -r '.[] | .mac' apinfo.json 
00:1F:64:CE:F3:8E

$ jq -r '.[] |.mac, .gpsSN' apinfo.json 
00:1F:64:CE:F3:8E
000000

jq -r '.[].mac, .[].gpsSN' apinfo.json 
00:1F:64:CE:F3:8E
000000
参考1.https://stedolan.github.io/jq/download/ 2.http://blog.csdn.net/u011641885/article/details/45559031