Shell脚本处理JSON数据工具jq 2015-02-17 10:00

基本介绍

  • 安装:

64位OS:

#wget http://stedolan.github.io/jq/download/linux64/jq
#chmod +x jq

32位OS:

#wget http://stedolan.github.io/jq/download/linux32/jq
#chmod +x jq
  • 使用:

有如下文档a.json:

{
      "action":"DescribeKeyPairsResponse",
      "total_count":2,
      "keypair_set":[
        {
          "description":null,
          "encrypt_method":"ssh-rsa",
          "keypair_name":"kp 1",
          "instance_ids":[
            "i-ogbndull"
          ],
          "create_time":"2013-08-30T05:13:50Z",
          "keypair_id":"kp-bn2n77ow",
          "pub_key":"AAAAB3..."
        },
        {
          "description":null,
          "encrypt_method":"ssh-rsa",
          "keypair_name":"kp 2",
          "create_time":"2013-08-31T05:13:50Z",
          "keypair_id":"kp-b2ivaf15",
          "pub_key":"AAAAB3..."
        }
      ],
      "ret_code":0
    }

使用:

cat a.json | ./jq ".keypair_set[].keypair_id"
  • 手册

stedolan.github.io/jq/manual/

Tags: #Shell #json    Post on Linux